本文介绍: 版本没设太高,否则会报错

Sass 官网

安装插件

注意 sass-loader 版本没设太高,否则会报错 Syntax Error: TypeError: this.getOptions is not a function

npm i sass sass-loader@10 -D

创建 Sass 文件

variables.module.scss注意这里module.scss

$testBg01: red;
$testBg02: blue;
// the :export directive is the magic sauce for webpack
// https://www.bluematador.com/blog/how-to-share-variables-between-js-and-sass
:export {
    testBg01: $testBg01;
    testBg02: $testBg02;
}

vue使用

<template&gt;
    <div&gt;
        <div class="box box01"&gt;</div&gt;
        <div class="box box02"></div>
    </div>
</template>

<script>
import variables from '../assets/variables.module.scss';

export default {
    mounted() {
        
        console.log('variables', variables);
    }
}
</script>
<style lang="scss" scoped>
@import '../assets/variables.module.scss';

.box {
    display: inline-block;
    width: 100px;
    height: 100px;
}
.box01 {
    background: $testBg01;
}
.box02 {
    background: $testBg02;
}
</style>

控制台输出
在这里插入图片描述

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注