我正在尝试从.vue
文件访问SCSS文件中定义的变量。该项目正在使用vue-cli。
根据Vue的文档:
“ Vue CLI项目附带对PostCSS,CSS模块和包括Sass,Less和Stylus在内的预处理器的支持。”
但是,如果我创建了variables.css
一个名为的变量的文件variable
,并尝试将其导入脚本中,variable
则找不到该文件。
styles / variables.module.css
$variable: 'foo';
:export {
variable: $variable
}
查看应用
<script>
import variables from "./styles/variables.module.scss";
export default {
name: "App",
methods: {},
computed: {
variable() {
console.log(variables); // Object {}
return variables.variable || "not found";
}
}
};
</script>
<style module>
但是,可以在同一vue文件的标签中导入variables.css文件。
查看应用
<style module lang="scss">
@import "./styles/variables.module.scss";
:export {
variable: $variable;
}
</style>
<p>Importing within <script>, the variable is {{variable}}</p>
// 'not found', should be "foo"
<p>Importing within <style>, the variable is {{$style.variable}}</p>
// correctly showing "foo"
.module
到SCSS文件名(根据vue的docs)requireModuleExtension: false
(来自相同文档)创建vue.config.js文件https://codesandbox.io/s/importing-css-to-js-o9p2b?file=/src/App.vue
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句