您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)vue-loader的使用方法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
.vue格式的文件使用類似HTML的語法描述vue組件。每個.vue文件包含三種最基本的語言塊:,
<template> <div class="example">{{ msg }}</div> </template> <script> export default { data () { return { msg: 'Hello world!' } } } </script> <style> .example { color: red; } </style>
vue-loader會解析這個文件中的每個語言塊,然后傳輸?shù)狡渌膌oaders,最終輸出到module.exports是vue組件的配置對象的CommonJS模塊。
vue-loader通過指定語言塊的lang屬性支持css預(yù)編譯、html編譯模板等語言格式。如在組件的style塊中使用sass
<style lang="sass"> /* write SASS! */ </style>
語言塊
默認語言:html
每個*.vue最多包含一個塊
塊中的內(nèi)容作為字符串提取出來
src 引入
如果你習(xí)慣將*.vue組件分割成多個文件,可以使用語言塊的src屬性把擴展文件引入到組件中。
<template src="./template.html"></template> <style src="./style.css"></style> <script src="./script.js"></script>
語法高亮
在編輯器中可以將*.vue文件作為HTML處理,實現(xiàn)語法高亮
使用 vue-cli
推薦vue-cli和vue-loader組合使用搭建項目
npm install -g vue-cli vue init webpack-simple hello-vue cd hello-vue npm install npm run dev # ready to go!
ES2015
當(dāng)vue-loader在同一個項目中檢測到babel-loader或者buble-loader的存在時,會用他們來處理*.vue文件中
<script> import ComponentA from './ComponentA.vue' import ComponentB from './ComponentB.vue' export default { components: { ComponentA, ComponentB } } </script>
我們可以使用ES2015對象的簡寫來定義子組件,{ ComponentA }是{ ComponentA: ComponentA }的簡寫。vue將會自動把鍵轉(zhuǎn)換為component-a,是以我們可以在中引入組件。
ES2015
*.vue文件的的內(nèi)容會被編譯進js渲染函數(shù),經(jīng)過 Buble等支持ES2015特性的自定義生成工具處理。所以我們可以使用Object shorthand properties 和 computed properties等ES2015特性。
<div :class="[{ active: active }, isButton ? prefix + '-button' : null]">
可以簡寫成:
<div :class="{ active, [`${prefix}-button`]: isButton }">
可以用buble自定義模板的特性支持
處理普通js文件
由于vue-loader只處理*.vue文件,需要在webpack的配置文件中配置babel-loader或者buble-loader來處理普通的js文件。vue-cli在項目中可以做這些事情。
在.babelrc文件中配置babel
局部css
當(dāng)一個style標簽帶有scoped屬性,它的css只應(yīng)用于當(dāng)前組件的元素。
<style scoped> .example { color: red; } </style> <template> <div class="example">hi</div> </template>
轉(zhuǎn)換為:
<style> .example[_v-f3f3eg9] { color: red; } </style> <template> <div class="example" _v-f3f3eg9>hi</div> </template>
注:
1 . 在同一個組件可以包含局部和全局樣式
<style> /* global styles */ </style> <style scoped> /* local styles */ </style>
子組件的根節(jié)點會受到父組件和本組件的局部css樣式影響
Partials are not affected by scoped styles.
有了局部樣式仍然需要類選擇器
在包含迭代組件的組件中小心使用子孫選擇器。一條關(guān)于.a .b的css規(guī)則,如果在類名為a的標簽中使用了子組件,那么子組件中的類名為b的標簽也會應(yīng)用這條規(guī)則。
CSS 模塊化
英文教程
CSS Modules便于實現(xiàn)css模塊化,vue-loader通過模仿css的scope提供了module來實現(xiàn)css模塊化集成。
使用在
<style module> .red { color: red; } .bold { font-weight: bold; } </style>
這樣打開CSS Module模式,class對象會作為$style的屬性注入到組件中,進而在中進行動態(tài)的類綁定
<template> <p :class="$style.red"> This should be red </p> </template>
style中的類作為被計算的屬性,也可以在:class中使用數(shù)組或者對象語法
<template> <div> <p :class="{ [$style.red]: isRed }"> Am I red? </p> <p :class="[$style.red, $style.bold]"> Red and bold </p> </div> </template>
或者在js中獲取使用它
<script> export default { created () { console.log(this.$style.red) // -> "_1VyoJ-uZOjlOxP7jWUy19_0" // an identifier generated based on filename and className. } } </script>
自定義注入名
由于一個vue組件可以包含多個
<style module="a"> /* identifiers injected as $a */ </style> <style module="b"> /* identifiers injected as $b */ </style>
配置css-loader
用css-loader來處理CSS Modules,以下是css-loader對
{ modules: true, importLoaders: true, localIdentName: '[hash:base64]' }
通過vue-loader的cssModules配置項定制css-loader
// wepback 1 vue: { cssModules: { // overwrite local ident name localIdentName: '[path][name]---[local]---[hash:base64:5]', // enable camelCase camelCase: true } } // webpack 2 module: { rules: [ { test: '\.vue$', loader: 'vue', options: { cssModules: { localIdentName: '[path][name]---[local]---[hash:base64:5]', camelCase: true } } } ] }
PostCSS
任何vue-loader處理輸出的css都經(jīng)過PostCSS進行局部css重寫,我們也可以增加PostCSS插件進行這些處理,如autoprefixer和 CSSNext。
Webpack 1.x用法:
// webpack.config.js module.exports = { // other configs... vue: { // use custom postcss plugins postcss: [require('postcss-cssnext')()] } }
Webpack 2.x用法:
// webpack.config.js module.exports = { // other configs... plugins: [ new webpack.LoaderOptionsPlugin({ vue: { // use custom postcss plugins postcss: [require('postcss-cssnext')()] } }) ] }
postcss也支持插件數(shù)組
postcss: { plugins: [...], // list of plugins options: { parser: sugarss // use sugarss parser } }
熱加載
熱加載不只是修改文件后頁面的刷新。修改某個.vue組件后,頁面中這個組件的所有實例都會被替換而不重載頁面,它還保存了應(yīng)用的當(dāng)前狀態(tài)以及被替換的組件。
感謝各位的閱讀!關(guān)于“vue-loader的使用方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。