上一篇文章中講到,如何在vue 項目中移入全局sass變量,那麼定義的變量是否只能在 css 中使用呢, 答案是不, 可以在js 中使用, 很厲害的。
首先需要安裝
> npm install sass-loader node-sass webpack --save-dev
在 vueconfig 中
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader', 'sass-loader']
}
]
}
最關鍵的是這裏 在CSS Module 有一個:export關鍵字,它在功能上等同於 ES6 的關鍵字export,即導出一個 js 對象。
:export {
contentLayoutL: $layout-xl;
}
接下來 在 組件裏引入
import egcommonCss from "../../common.scss";
console.log(egcommonCss.contentLayoutL)
ok 大功告成了