网络编程
位置:首页>> 网络编程>> JavaScript>> 详解Vue 全局引入bass.scss 处理方案

详解Vue 全局引入bass.scss 处理方案

作者:Bboy_2016  发布时间:2024-06-05 10:03:08 

标签:vue,全局引入,scss

为解决在Vue组件中全局引入 scss 变量及 mixins ,装载了一个名为 "sass-resources-loader" 解析器。

安装


$ > cnpm i -D sass-resources-loader

配置

配置的话是在 vue 的 loader 解析器中配置,即在 vue-cli 脚手架方式构建出来的文件中是以 build/utils.js,在该文件中定义了 cssLoaders() 方法,该方法定义了诸如 css、less的解析方式,具体如下。


// build/utils.js
exports.cssLoaders = function (options) {
options = options || {}

...
return {
 // ...
 sass: generateLoaders('sass', { indentedSyntax: true }),
 // 定义在这里 =======================》
 scss: generateLoaders('sass').concat(
  {
   loader: 'sass-resources-loader',
   options: {
    resources: path.resolve(__dirname, '../src/common/style/global.scss') // 需要全局引入的文件
   }
  }
 ),
 // 定义在这里 =======================》
 // ...
}
}

// global.scss
@mixin line-height($height) {
height: $height;
line-height: $height;
}
$head-height: .45rem;

// xx.vue
<style lang="scss">
height: @include line-height(45px);
</style>

建议

在 global.scss 中只需要定义变量 或者是 mixins (混合)。

参考

https://vue-loader.vuejs.org/zh-cn/configurations/pre-processors.html

来源:https://www.jianshu.com/p/73a156610c18

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com