我的 Vue 项目结构: styles 目录包含所有CSS文件。
src
├── assets
├── components
├── styles
│ ├── main.css
│ ├── normalize.css
│ └── variables.css
├── views
├── App.vue
├── main.ts
├── router.ts
src / styles / main.css 导入其他CSS,就像:
@import 'normalize.css';
@import 'variables.css';
我想在Vue全局中导入src/styles/main.css
。那就是:
import Vue from 'vue';
import './styles/main.css'; // global
import App from './App.vue';
import router from './router';
Vue.config.productionTip = false;
new Vue({
router,
render: (h) => h(App),
}).$mount('#app');
然后,我可以直接在每个 .vue 组件的 variables.css 中定义的变量。喜欢:
<style lang="postcss" scoped>
a {
color: $primary; // defined in variables.css
}
</style>
但是postcss-loader
无法解析$primary
变量:
Module build failed (from ./node_modules/postcss-loader/lib/index.js):
Syntax Error
(79:10) Could not resolve the variable "$primary" within "$primary"
如何导入全局main.css
,所以不需要在每个@import 'variables.css'
组合中都.vue
?
答案 0 :(得分:0)
我认为PostCss没有像“ @variable”这样的可变语法。看起来像SASS或LESS。 我认为您必须安装一个单独的插件才能使此类变量与PostCSS一起使用: https://www.npmjs.com/package/postcss-variables
PostCSS使用以下语法: https://github.com/postcss/postcss-custom-properties
编辑:
好吧,经过一番研究,我发现了以下信息:https://github.com/vuejs/vue-loader/issues/328
我非常确定这对于postcss-custom-properties也有效。因此,您将必须在每个组件中导入变量。