国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

深入理解vue-loader如何使用

2019-11-19 16:24:47
字體:
來源:轉載
供稿:網友

.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會解析這個文件中的每個語言塊,然后傳輸到其它的loaders,最終輸出到module.exports是vue組件的配置對象的CommonJS模塊。

vue-loader通過指定語言塊的lang屬性支持css預編譯、html編譯模板等語言格式。如在組件的style塊中使用sass

<style lang="sass"> /* write SASS! */</style>

語言塊

  1. 默認語言:html
  2. 每個*.vue最多包含一個塊
  3. 塊中的內容作為字符串提取出來

src 引入

如果你習慣將*.vue組件分割成多個文件,可以使用語言塊的src屬性把擴展文件引入到組件中。

<template src="./template.html"></template><style src="./style.css"></style><script src="./script.js"></script>

語法高亮

在編輯器中可以將*.vue文件作為HTML處理,實現語法高亮

使用 vue-cli

推薦vue-cli和vue-loader組合使用搭建項目

npm install -g vue-clivue init webpack-simple hello-vuecd hello-vuenpm installnpm run dev # ready to go!

ES2015

當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將會自動把鍵轉換為component-a,是以我們可以在中引入組件。

ES2015

*.vue文件的的內容會被編譯進js渲染函數,經過 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

當一個style標簽帶有scoped屬性,它的css只應用于當前組件的元素。

<style scoped>.example { color: red;}</style><template> <div class="example">hi</div></template>

轉換為:

<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>
  1. 子組件的根節點會受到父組件和本組件的局部css樣式影響
  2. Partials are not affected by scoped styles.
  3. 有了局部樣式仍然需要類選擇器
  4. 在包含迭代組件的組件中小心使用子孫選擇器。一條關于.a .b的css規則,如果在類名為a的標簽中使用了子組件,那么子組件中的類名為b的標簽也會應用這條規則。

CSS 模塊化

英文教程

CSS Modules便于實現css模塊化,vue-loader通過模仿css的scope提供了module來實現css模塊化集成。

使用在

<style module>.red { color: red;}.bold { font-weight: bold;}</style>

這樣打開CSS Module模式,class對象會作為$style的屬性注入到組件中,進而在中進行動態的類綁定

<template> <p :class="$style.red">  This should be red </p></template>

style中的類作為被計算的屬性,也可以在:class中使用數組或者對象語法

<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 1vue: { cssModules: {  // overwrite local ident name  localIdentName: '[path][name]---[local]---[hash:base64:5]',  // enable camelCase  camelCase: true }}// webpack 2module: { rules: [  {   test: '/.vue$',   loader: 'vue',   options: {    cssModules: {     localIdentName: '[path][name]---[local]---[hash:base64:5]',     camelCase: true    }   }  } ]}

PostCSS

任何vue-loader處理輸出的css都經過PostCSS進行局部css重寫,我們也可以增加PostCSS插件進行這些處理,如autoprefixer和 CSSNext。

Webpack 1.x用法:

// webpack.config.jsmodule.exports = { // other configs... vue: {  // use custom postcss plugins  postcss: [require('postcss-cssnext')()] }}

Webpack 2.x用法:

// webpack.config.jsmodule.exports = { // other configs... plugins: [  new webpack.LoaderOptionsPlugin({   vue: {    // use custom postcss plugins    postcss: [require('postcss-cssnext')()]   }  }) ]}

postcss也支持插件數組

postcss: { plugins: [...], // list of plugins options: {  parser: sugarss // use sugarss parser }}

熱加載

熱加載不只是修改文件后頁面的刷新。修改某個.vue組件后,頁面中這個組件的所有實例都會被替換而不重載頁面,它還保存了應用的當前狀態以及被替換的組件。


如果使用了vue-cli搭建項目,自帶了熱加載。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桃园市| 民权县| 定兴县| 汤原县| 永靖县| 和静县| 沿河| 永定县| 内丘县| 巧家县| 方城县| 松桃| 江孜县| 德兴市| 民县| 麻栗坡县| 普格县| 延川县| 甘谷县| 绥化市| 武强县| 鄯善县| 皋兰县| 封开县| 宁化县| 新竹县| 麻栗坡县| 河池市| 霍州市| 安多县| 聂拉木县| 明水县| 宿州市| 邓州市| 平陆县| 辽源市| 尖扎县| 马鞍山市| 永年县| 祁东县| 格尔木市|