SASS全球变量和混合物不起作用

发布于 2025-01-30 18:22:14 字数 1609 浏览 0 评论 0原文

我已经使用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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半暖夏伤 2025-02-06 18:22:14

在您的vite配置中使用

@import

而不是

@use

vite.config.ts

export default defineConfig({
    plugins: [vue()],
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: '@import "./src/styles/variables.scss";',
            },
        },
    },
});

请记住,您不能再次在main.ts main.tsvariables.scss file否则,您将

[sass] This file is already being loaded.

通过您提到的每个单个组件中的每个组件中的每个组件都会导入此错误,但这确实很乏味,因此在<<代码> vite.config.ts在全球使用的文件中,例如variables.scss.scss文件,Preprocessoroptions 是一个更好的选择。

use

@import

in your vite config instead of

@use

vite.config.ts:

export default defineConfig({
    plugins: [vue()],
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: '@import "./src/styles/variables.scss";',
            },
        },
    },
});

keep in mind that you cannot import the same file variables.scss again in your main.ts file otherwise, you will get this error

[sass] This file is already being loaded.

by 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 in preprocessorOptions in vite.config.ts is a much better option for files used globally like a variables.scss file.

唔猫 2025-02-06 18:22:14

我设法“解决”了这个问题。事实证明,当我替换所有@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 the additionalData key from config and include the imports manually.

粉红×色少女 2025-02-06 18:22:14

您不是尝试将添加为 * frops 值吗?
这样一行看起来就是这样:

scss: {
    additionalData: '@use "@/styles/variables" as *;',
  },

这对我有用SCSS Mixins对我来说是有效的,我更喜欢此解决方案,因为使用@import而不是@USE这里确实迫使我交换所有@USE s @Import在VUE组件中 s(因为“ @Import规则在@USE之前不能放置@Import规则”错误),这有一些缺点。

Haven't you tried to add as * to your additionalData value?
So that the line would look like this:

scss: {
    additionalData: '@use "@/styles/variables" as *;',
  },

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 @uses to @imports in Vue component (because of "@import rules cannot be placed before @use" error), which has some drawbacks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文