【发布时间】:2021-03-02 20:44:02
【问题描述】:
我目前正在为此苦苦挣扎。在我的 Symfony 项目中,我有一个 _variables.scss 文件,其中保存了我的全局变量(例如颜色)。
这包含在我的主 scss 文件中,例如 @import "variables"; - 工作正常。现在我也在我的项目中使用 VueJs,我想在 Vue 组件中使用我的全局变量。现在实现这一点的一种方法就是导入 variables.scss 本身:
//CustomButton.vue
<template>
...
</template>
<script>
export default {
name: "CustomButton"
}
</script>
<style lang="scss" scoped>
@import "../../scss/_variables.scss";
//able to use variables here
</style>
但是随着项目的不断增长和不同的路径,这似乎是不必要的工作。我想要实现的是自动将variables.scss加载到每个Vue组件中。
这里有很好的解释:[css-tricks.com - How to Import a Sass File into Each Vue Component in an App][1]
遗憾的是,这在我的情况下不起作用(我认为 vue.config.js 被完全忽略) - 变量在 Vue 组件中仍然不可用。我还尝试将 JavaScript 添加到我的主 js 文件中——我在其中加载 Vue——但这似乎破坏了一些东西(一些模块异常)。
有什么具体的方法可以用 symfony 实现这一点吗?
PS:我正在使用 Symfony 5、Sass-Loader 9.0.1、Vue 2.6.12、Vue-Loader 15、Vue-Template-Compiler 2.6.12 [1]:https://css-tricks.com/how-to-import-a-sass-file-into-every-vue-component-in-an-app/
解决方案 1:
//webpack.config.js
.enableSassLoader(options => {
options.additionalData = `
@import "./assets/scss/_variables.scss"; //path.resolve is not working in my case to import the absolute path
`
})
```
【问题讨论】:
-
你用什么来编译你的代码?因为你使用的是 Symfony,我想你可能会使用 Encore,对吧?
-
@H3lltronik 是的,再来一次
-
@Syllz,看起来您需要使用 Encore 文档的 symfony.com/doc/current/frontend/encore/… 这部分来调整我的配置示例以满足您的需求。不幸的是,我对 symfony Encore 并不熟悉。
-
添加此代码后是否重新启动了开发服务器?因为我为我工作