关于scss:Vuecli中使用scss-的全局变量和-mixin

12次阅读

共计 1168 个字符,预计需要花费 3 分钟才能阅读完成。

装置 scss

npm  install sass-loader@10.1.1 --save-dev
npm install node-sass --sava-dev

留神:sass-loader 须要指定 @10 的版本,因为后续的版本在 vue-cli 脚手架中会呈现一些问题

根底应用

<style lang="scss" scoped>
  .xxxx {
      .xxx-x {...}
  }
</style>

大部分场景下,应用 scss 能够实现下面的款式嵌套层级关系,置信大家都用过。

上面要说下 scss 的进阶用法。scss 全局变量和 mixin。

环境配置

想要在 vue-cli 中全局应用 scss 的全局变量和 @mixin 款式混入,须要装置插件,而后在 vue.conf.js 中配置

npm install style-resources-loader vue-cli-plugin-style-resources-loader --save-dev

vue.config.js  中配置

module.exports = {
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'scss',
      patterns: [
        // 门路依据具体需要更改
        path.resolve(__dirname, 'src/assets/styles/variables.scss'),
        path.resolve(__dirname, 'src/assets/styles/mixin.scss')
      ]
    }
  }
}

scss 全局变量的应用

上述环境配置中配置的 variables.scss 就是全局的变量文件

variables.scss 

$--color-primary: #468fff;
$--color-primary-active: #69a5ff;

xxx.vue 文件中间接应用该变量

<style lang="scss" scoped>
.main-wrap {background: $--color-primary;}
</style>

@mixin 与 @include 实现 css 编程式格调

mixin.scss 
@mixin 函数名($ 参数名: 默认值)

@mixin flex($justify-content: center, $align-center: center, $flex-direction: row){
  display: flex;
  justify-content: $justify-content;
  align-items: $align-center;
  flex-direction: $flex-direction;
}

xxx.vue 中应用
应用 @include 进行援用即可

<style lang="scss" scoped>
.main-wrap {@include flex(center,center,row);
}
</style>
正文完
 0