Vue
Vue.js 是一个渐进式、可增量采用的 JavaScript 框架,用于在 Web 上构建 UI。Parcel 使用 @parcel/transformer-vue
插件自动支持 Vue。当检测到 .vue
文件时,它将自动安装到你的项目中。
¥Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web. Parcel supports Vue automatically using the @parcel/transformer-vue
plugin. When a .vue
file is detected, it will be installed into your project automatically.
用法示例
#¥Example usage
<!DOCTYPE html>
<div id="app"></div>
<script type="module" src="./index.js"></script>
import { createApp } from "vue";
import App from "./App.vue";
const app = createApp(App);
app.mount("#app");
<template>
<div>Hello {{ name }}!</div>
</template>
<script>
export default {
data() {
return {
name: "Vue",
};
},
};
</script>
HMR
#Parcel 使用官方 Vue SFC 编译器,它支持开箱即用的 HMR,因此你将获得快速、反应式的开发体验。有关 Parcel 中 HMR 的更多详细信息,请参阅 热重载。
¥Parcel uses the official Vue SFC compiler, which supports HMR out of the box so you'll have a fast, reactive development experience. See Hot reloading for more details about HMR in Parcel.
Vue 3 特性
#¥Vue 3 Features
由于 Parcel 使用 Vue 3,因此你可以使用所有 Vue 3 功能,例如 合成 API。
¥Since Parcel uses Vue 3, you can use all Vue 3 features, such as the Composition API.
<template>
<button @click="increment">
Count is: {{ state.count }} Double is: {{
state.double }}
</button>
</template>
<script>
import { reactive, computed } from "vue";
export default {
setup() {
const state = reactive({
count: 0,
double: computed(() => state.count * 2),
});
function increment() {
state.count++;
}
return {
state,
increment,
};
},
};
</script>
语言支持
#¥Language Support
Parcel 支持 JavaScript、TypeScript 和 CoffeeScript 作为 Vue 中的脚本语言。
¥Parcel supports JavaScript, TypeScript, and CoffeeScript as scripting languages in Vue.
几乎任何模板语言(consolidate 支持的所有模板语言)都可以使用。
¥Almost any templating language (all those supported by consolidate) can be used.
对于样式,支持 Less、Sass 和 Stylus。此外,CSS 模块 和 范围样式 可以与 module
和 scoped
修饰符一起使用。
¥For styling, Less, Sass, and Stylus are supported. In addition, CSS Modules and scoped style can be used with the module
and scoped
modifiers.
<style lang="scss" scoped>
/* This style will only apply to this module */
$red: red;
h1 {
background: $red;
}
</style>
<style lang="less">
@green: green;
h1 {
color: @green;
}
</style>
<style src="./App.module.css">
/* The content of blocks with a `src` attribute is ignored and replaced with
the content of `src`. */
</style>
<template lang="pug"> div h1 This is the app </template>
<script lang="coffee">
module.exports =
data: ->
msg: 'Hello from coffee!'
</script>
自定义块
#¥Custom Blocks
你可以在 Vue 组件中使用自定义块,但必须使用 .vuerc
、vue.config.js
等配置 Vue 以定义如何预处理这些块。
¥You can use custom blocks in your Vue components, but must configure Vue with .vuerc
, vue.config.js
, etc. to define how you will preprocess those blocks.
{
"customBlocks": {
"docs": "./src/docs-preprocessor.js"
}
}
export default function (component, blockContent, blockAttrs) {
if (blockAttrs.brief) {
component.__briefDocs = blockContent;
} else {
component.__docs = blockContent;
}
}
<template>
<div>Home Page</div>
</template>
<docs> This component represents the home page of the application. </docs>
<docs brief> Home Page </docs>
<template>
<div>
<child></child>
docs: {{ docs.standard }} in brief: {{
docs.brief }}
</div>
</template>
<script>
import Child from "./HomePage.vue";
export default {
components: {
child: Child,
},
data() {
let docs = { standard: Child.__docs, brief: Child.__briefDocs };
return { docs };
},
};
</script>