SASS全球变量和混合物不起作用
我已经使用VUE 3.2.33和VITE 2.9.5设置了一个项目,
当我尝试从任何VUE组件中访问任何全局变量或Mixin时,我会遇到一个不确定的错误。此问题不会在SCSS文件中发生。
该导入本身似乎正常工作,因为其中任何CSS规则都在起作用。
vite.config.ts:
import { fileURLToPath, URL } from 'url';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
css: {
preprocessorOptions: {
scss: {
additionalData: '@use "@/styles/variables";',
},
},
},
});
src/styles/_variables.scss:
// breakpoints
$breakpoints: (
"sm": 576px,
"md": 768px,
"lg": 992px,
"xl": 1200px,
"xxl": 1400px,
);
@mixin test {
border: 3px solid red;
}
示例使用:
<style scoped lang="scss">
@use 'sass:map';
.container {
max-width: 100%;
width: 100%;
margin: 0 auto;
@include test; // <- undefined
&--fluid {
max-width: 100%;
width: 100%;
}
}
$widths: (
'sm': 540px,
'md': 720px,
'lg': 960px,
'xl': 1140px,
'xxl': 1320px,
);
@each $breakpoint, $width in $widths {
@media (min-width: map.get($breakpoints, $breakpoint)) { // <- $breakpoints undefined
.container {
max-width: $width;
}
}
}
</style>
I've set up a project using Vue 3.2.33 and Vite 2.9.5
When I try to access any global variable or mixin from within any vue component, I get an undefined error. This problem doesn't occur in scss files.
The import itself seems working correctly because any css rules in it are working.
vite.config.ts:
import { fileURLToPath, URL } from 'url';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
},
},
css: {
preprocessorOptions: {
scss: {
additionalData: '@use "@/styles/variables";',
},
},
},
});
src/styles/_variables.scss:
// breakpoints
$breakpoints: (
"sm": 576px,
"md": 768px,
"lg": 992px,
"xl": 1200px,
"xxl": 1400px,
);
@mixin test {
border: 3px solid red;
}
Example use:
<style scoped lang="scss">
@use 'sass:map';
.container {
max-width: 100%;
width: 100%;
margin: 0 auto;
@include test; // <- undefined
&--fluid {
max-width: 100%;
width: 100%;
}
}
$widths: (
'sm': 540px,
'md': 720px,
'lg': 960px,
'xl': 1140px,
'xxl': 1320px,
);
@each $breakpoint, $width in $widths {
@media (min-width: map.get($breakpoints, $breakpoint)) { // <- $breakpoints undefined
.container {
max-width: $width;
}
}
}
</style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的vite配置中使用
而不是
vite.config.ts
:请记住,您不能再次在
main.ts
main.tsvariables.scss
file否则,您将通过您提到的每个单个组件中的每个组件中的每个组件都会导入此错误,但这确实很乏味,因此在<<代码>
vite.config.ts
在全球使用的文件中,例如variables.scss.scss
文件,Preprocessoroptions 是一个更好的选择。use
in your vite config instead of
vite.config.ts
:keep in mind that you cannot import the same file
variables.scss
again in yourmain.ts
file otherwise, you will get this errorby the way, you can also import the
scss
file in every single component manually as you mentioned but that would be really tedious so using a global import inpreprocessorOptions
invite.config.ts
is a much better option for files used globally like avariables.scss
file.我设法“解决”了这个问题。事实证明,当我替换所有
@USE
文件导入规则时,SASS代码是正确导入并有效的。但这会产生一个新问题,因为@import
不能在@USE
之前放置规则,因此我必须从配置和手动包括进口。I've managed to "fix" the issue. Turns out, when I replace all
@use
rules for file imports, the sass code is imported correctly and works. But this produces a new problem as the@import
rules cannot be placed before@use
, so I had to remove theadditionalData
key from config and include the imports manually.您不是尝试将
添加为 *
frops 值吗?这样一行看起来就是这样:
这对我有用SCSS Mixins对我来说是有效的,我更喜欢此解决方案,因为使用
@import
而不是@USE
这里确实迫使我交换所有@USE
s@Import
在VUE组件中 s(因为“ @Import规则在@USE之前不能放置@Import规则”错误),这有一些缺点。Haven't you tried to add
as *
to youradditionalData
value?So that the line would look like this:
This worked for me with scss mixins and I prefer this solution because using
@import
instead of@use
here indeed forces me to swap all@use
s to@import
s in Vue component (because of "@import rules cannot be placed before @use" error), which has some drawbacks.