Vue
Vue.js 是一個漸進式、可逐步採用的 JavaScript 框架,用於在網路上建構 UI。Parcel 使用 @parcel/transformer-vue
外掛自動支援 Vue。偵測到 .vue
檔案時,它會自動安裝到您的專案中。
注意:Parcel 不支援在 Vue 2 中使用 SFC,您必須使用 Vue 3 或更新版本。
範例用法
#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。
Vue 3 功能
#由於 Parcel 使用 Vue 3,您可以使用所有 Vue 3 功能,例如 組合 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>
語言支援
#Parcel 支援 JavaScript、TypeScript 和 CoffeeScript 作為 Vue 中的指令碼語言。
幾乎任何樣板語言(所有受 consolidate 支援的語言)都可以使用。
對於樣式,支援 Less、Sass 和 Stylus。此外,CSS 模組 和 範圍樣式 可以與 module
和 scoped
修飾詞一起使用。
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>
自訂區塊
#您可以在 Vue 元件中使用自訂區塊,但必須使用 .vuerc
、vue.config.js
等設定 Vue,以定義如何預處理這些區塊。
.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>