详解Vue2 添加对scss的支持
引入loader
cnpm install node-sass --save-dev cnpm install sass-loader --save-dev cnpm install style-loader --save-dev
scss支持
为了使用scss,我们需要先安装相关的node包:
npm install --save style-loader sass-loader node-sass file-loader
安装好之后,为了可以在.vue和.scss中使用,需要在webpack.config.js中配置对应的解析器:
var webpack = require('webpack'); module.exports = { entry: ['./src/entry.js'], output: { path: __dirname, filename: 'build/main.js' }, resolve: { alias: { 'vue': 'vue/dist/vue.js' } }, module: { loaders: [{ test: /\.vue$/, loader: 'vue-loader', options: { loaders: { scss: 'style-loader!css-loader!sass-loader' } } }, { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },{ test: /\.css$/, loader: ['style-loader', 'css-loader'] }, { test: /\.scss$/, loader: ['style-loader', 'css-loader', 'sass-loader'] }, { test: /\.(png|jpg|jpeg|gif|bmp)$/, loader: ['file-loader?limit=7000&name=build/assets/[name].[ext]'] } ] } };
关于上面resolve的配置,是因为编译和打包源文件默认不一样。
测试scss
打开PageTwo.vue测试一下:
<style lang="scss" scoped> section { & > div { font-size: 20px; } } </style>
编译正常。
本篇最终项目文件Github地址:github.com/yelloxing/vue.quick/tree/V6
相关推荐
前端 2020-08-03
maiktom 2020-05-11
StylusGalaxy 2020-04-29
前端 2020-04-29
骷髅狗 2020-04-24
jiangfulai 2020-04-18
jiangfulai 2020-04-11
jiangfulai 2020-03-05
taoqidejingling 2020-03-05
ZylCN 2020-03-03
前端 2020-02-19
不知道写什么 2020-01-16
xxuncle 2019-12-27
zhanghao 2019-12-27
wghou 2019-12-27
王军强 2019-11-17
ZylCN 2019-11-12
tianzyc 2019-11-08