我正在使用 VSCode 和 vue。 我可以通过 import 语句将外部 scss 文件与 vue sfc 中的一些变量一起使用。除了智能感知不加载 .vue 文件中的变量之外,一切正常。
这是我在 .vue 文件中的样式标签
<style lang="scss" scoped>
@import '~styles/main';
$other-color: #FFAAAA;
.greeting {
font-size: 20px;
color: $secondary-color;
border-bottom: 2px solid $primary-color;
margin-bottom: 15px;
}
</style>
$other-color 是由智能感知找到的,但不是在 '~styles/main' 中定义的 $primary-color 和 $secondary-color (并由 webpack 正确加载)。
我是否遗漏了什么或者无法使其工作?
我首先创建了一个单独的 .scss 文件,将所有组件样式移至此处,然后在我的 vue sfc 文件中添加了对组件 scss 的引用。
<!-- .vue -->
<template>
<div>
<div class="greeting">Hello {{name}}{{exclamationMarks}}</div>
<button @click="decrement">-</button>
<button @click="increment">+</button>
</div>
</template>
<script lang="ts">
import Vue from "vue";
export default Vue.extend({
props: ['name', 'initialEnthusiasm'],
data() {
return {
enthusiasm: this.initialEnthusiasm,
}
},
methods: {
increment() { this.enthusiasm++; },
decrement() {
if (this.enthusiasm > 1) {
this.enthusiasm--;
}
},
},
computed: {
exclamationMarks(): string {
return Array(this.enthusiasm + 1).join('!');
}
}
});
</script>
<style src="./Hello.scss" scoped></style>
通过这种方式,我可以毫无问题地使用导入的 scss 文件中的变量。
在此示例中,greeting类在 Hello.scss 文件中定义。为了在我的 vue 文件中自动完成 scss 类,我使用 this Visual Studio Code 扩展。