vue cli3 配置 stylus全局變量的使用方式
vue cli3配置stylus全局變量
首先在common.styl文件中寫一些常用的css變量,方便全局使用,避免重復代碼。
預想的是在main.js中引入common.styl文件,然后所有的.vue文件就都可以使用了。但是事與愿違,根本不起作用。
網絡上有很多教程,但是都不起作用(找答案是個痛苦且漫長的過程),下面是總結的代碼,基本開箱即用,希望能幫助到需要幫助的人。
// vue.config.js module.exports = { // 配置使用stylus全局變量 chainWebpack: config => { const types = ["vue-modules", "vue", "normal-modules", "normal"]; types.forEach(type => addStyleResource(config.module.rule("stylus").oneOf(type)) ); } } // 定義函數addStyleResource function addStyleResource(rule) { rule .use("style-resource") .loader("style-resources-loader") .options({ patterns: [path.resolve(__dirname, "./src/styles/common.styl")] //后面的路徑改成你自己放公共stylus變量的路徑 }); }
Tips: 使用vue cli3創(chuàng)建的項目默認是沒有vue.config.js文件的,需要自己在項目根目錄創(chuàng)建。
vue cli3使用stylus全局變量
在vue.config.js中添加以下代碼
const path = require('path') module.exports = { // 配置使用stylus全局變量 chainWebpack: config => { const types = ["vue-modules", "vue", "normal-modules", "normal"]; types.forEach(type => addStyleResource(config.module.rule("stylus").oneOf(type)) ); } }; // ====定義函數addStyleResource=== function addStyleResource(rule) { rule .use("style-resource") .loader("style-resources-loader") .options({ patterns: [path.resolve(__dirname, "./src/css/var.styl")] //后面跟著的路徑是你自己放公共stylus變量的路徑 }); }
保存文件,下載依賴即可
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue項目環(huán)境搭建?啟動?移植操作示例及目錄結構分析
這篇文章主要介紹了vue項目環(huán)境搭建、啟動、項目移植、項目目錄結構分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-04-04vuex中this.$store.commit和this.$store.dispatch的基本用法實例
在vue的項目里常常會遇到父子組件間需要進行數據傳遞的情況,下面這篇文章主要給大家介紹了關于vuex中this.$store.commit和this.$store.dispatch的基本用法的相關資料,需要的朋友可以參考下2023-01-01