V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
dongtingyue
V2EX  ›  问与答

vue 如何全局引入 scss 文件

  •  
  •   dongtingyue · 2023-01-13 11:27:31 +08:00 · 534 次点击
    这是一个创建于 441 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在 main 中这样引入了

    import '@/assets/scss/common.scss'
    

    common.scss 文件

    @import './_mixins.scss'; //中存在 @mixin scrollbar ($width: 6px) {
    @import './_vars.scss';
    

    在组件中使用时报错 No mixin named scrollbar

    <style lang="scss" scoped>
     @include scrollbar;
    

    再次引入才正常为啥?

    <style lang="scss" scoped>
    @import '@/assets/scss/_mixins.scss';
     @include scrollbar;
    
    iulo
        1
    iulo  
       2023-01-13 13:19:36 +08:00
    scss 块是独立编译的,你在 main 引入 mixin 并不能影响到 sfc 的 scss 块,参考下 vite 的 css 预编译 additionalData 配置,可以在每个 scss 文件前注入通用的工具 mixin/function/import 等,webpack 也有类似的插件

    https://vitejs.dev/config/shared-options.html#css-preprocessoroptions
    kylebing
        2
    kylebing  
       2023-01-13 17:16:20 +08:00
    全局跟文件内部的无关,可以想成是两个通道。我一般都把外部的通用方法和变量放到 _plugin.scss 中

    ```scss
    <style scoped lang="scss">
    @import "../../scss/plugin";

    ```

    具体可以参阅这里: https://github.com/KyleBing/diary/blob/master/src/page/invitation/InvitationList.vue
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2831 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 13:16 · PVG 21:16 · LAX 06:16 · JFK 09:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.