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.

注意:Parcel 不支持在 Vue 2 中使用 SFC,你必须使用 Vue 3 或更高版本。

¥Note: Parcel does not support using SFCs with Vue 2, you must use Vue 3 or later.

用法示例

#

¥Example usage

index.html:
<!DOCTYPE html>
<div id="app"></div>
<script type="module" src="./index.js"></script>
index.js:
import { createApp } from "vue";
import App from "./App.vue";

const app = createApp(App);
app.mount("#app");
App.vue:
<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.

App.vue:
<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 支持 JavaScriptTypeScriptCoffeeScript 作为 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.

对于样式,支持 LessSassStylus。此外,CSS 模块范围样式 可以与 modulescoped 修饰符一起使用。

¥For styling, Less, Sass, and Stylus are supported. In addition, CSS Modules and scoped style can be used with the module and scoped modifiers.

App.vue:
<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 组件中使用自定义块,但必须使用 .vuercvue.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.

.vuerc:
{
"customBlocks": {
"docs": "./src/docs-preprocessor.js"
}
}
src/docs-preprocessor.js:
export default function (component, blockContent, blockAttrs) {
if (blockAttrs.brief) {
component.__briefDocs = blockContent;
} else {
component.__docs = blockContent;
}
}
HomePage.vue:
<template>
<div>Home Page</div>
</template>

<docs> This component represents the home page of the application. </docs>

<docs brief> Home Page </docs>
App.vue:
<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>
Parcel 中文网 - 粤ICP备13048890号