vue 項目常用載入器及配置詳解,vue載入配置詳解

來源:互聯網
上載者:User

vue 項目常用載入器及配置詳解,vue載入配置詳解

本文介紹了vue 項目常用載入器及配置詳解,分享給大家,具體如下:

1.安裝sass:

1.1 由於sass-loader依賴於node-sass,所以在安裝sass-loader的同時還需安裝node-sass

npm install --save-dev node-sassnpm install --save-dev sass-loader

1.2 安裝完成後修改 <style>標籤:

 <style lang="scss"></style>

2.安裝axios:

axios用於資料請求,在Vue1.0的時候有一個官方推薦的 ajax 外掛程式 [vue-resource](https://github.com/pagekit/vue-resource),但是自從 Vue 更新到 2.0 之後,官方就不再更新 vue-resource,推薦使用axios。

2.1 安裝:

npm install axios --save-dev

2.2. 在main.js中引入:

import axios from 'axios'Vue.prototype.$http = axios

2.3. 在組件中使用:

this.$http({  method:'get',  url:'http://breadoffer.com/api/artcile',  params:{   platformCode:'pc'   // 用於向後台傳參  }}).then(response => {  console.log(response)})

3.安裝mock:

3.1. 安裝:

npm install mockjs --save-dev

3.2. 使用:在src下建立mock.js檔案

import Mock from 'mockjs';export default Mock.mock('http://platform.breadoffer.com/api/oversea', { "data":{  "breadActivities|9":[{   "title":"@csentence(5,25)",   "desc":"@paragraph(2)",   "beiginTime":"@date",   "endTime":"@date",   "stateName":"進行中",  }], }})

3.3. 在需要資料的組件中引入:

import datas from '../mock' // 根據自己實際目錄引入 methods: {  request() {  this.$http({   method: 'get',   url: 'http://platform.breadoffer.com/api/oversea',   params: {   courseMaxCount: 2,  //設定課程返回的資料為2條   teacherMaxCount: 10, //設定導師返回的資料為10條   }  }).then(response => {   console.log(response)  }).catch(error => {    console.log(error)  })  }, }

4.安裝 lib-flexible: --實現移動端自適應

4.1 安裝:

npm install lib-flexible --save

在實際開發過程中,使用flexible外掛程式時會自動把px轉換成rem單位,在vue項目中我們使用px2rem這個工具進行轉換,所以需要安裝px2rem載入器:

npm install px2rem-loader 

4.2 在main.js中引入:

import 'lib-flexible'

4.3 配置px2rem-loader: (在build/untils.js)

remUnit 的意思是1rem=多少像素, 結合lib-flexible,我們將px2remLoader的option.remUnit 設定成設計稿寬度的1/10,假設我們的設計稿寬度是750,則remUnit為75,然後在cssLoader後面加一個px2remLoader即可

 var px2remLoader = { loader: 'px2rem-loader', options: {  remUnit: 75 } } // generate loader string to be used with extract text plugin function generateLoaders (loader, loaderOptions) { const loaders = [cssLoader,px2remLoader] if (loader) {  loaders.push({  loader: loader + '-loader',  options: Object.assign({}, loaderOptions, {   sourceMap: options.sourceMap  })  }) }

5.安裝 sass-resourses-loader

如果在項目中使用sass,或多或少會用到全域的變數,mixin/function等,那麼如何將其設為全域狀態,以避免在每個vue檔案中引入?

5.1 安裝 sass-resources-loader:

npm i sass-resources-loader

5.2 在main.js中引入

import 'lib-flexible'

5.3 配置px2rem-loader: (在build/untils.js)

在檔案中找到這句注釋

// [https://vue-loader.vuejs.org/en/configurations/extract-css.html](https://vue-loader.vuejs.org/en/configurations/extract-css.html)

在注釋上面添加如下函數:

 function resolveResouce(name) {  return path.resolve(__dirname, '../src/sass/' + name);  // sass檔案所在目錄 } function generateSassResourceLoader() {  var loaders = [   cssLoader,   // 'postcss-loader',   'sass-loader',   {    loader: 'sass-resources-loader',    options: {     // it need a absolute path     resources: [resolveResouce('_mixin.scss')]    }   }  ];  if (options.extract) {   return ExtractTextPlugin.extract({    use: loaders,    fallback: 'vue-style-loader'   })  } else {   return ['vue-style-loader'].concat(loaders)  } }

並且將以下代碼

 // return { // css: generateLoaders(), // postcss: generateLoaders(), // less: generateLoaders('less'), // sass: generateLoaders('sass', { indentedSyntax: true }), // scss: generateLoaders('sass'), // stylus: generateLoaders('stylus'), // styl: generateLoaders('stylus') // }

替換為:

 return {  css: generateLoaders(),  postcss: generateLoaders(),  less: generateLoaders('less'),  sass: generateSassResourceLoader(),  scss: generateSassResourceLoader(),  stylus: generateLoaders('stylus'),  styl: generateLoaders('stylus') }

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.