在VueJS组件中使用sass变量

时间:2016-02-23 14:53:40

标签: laravel sass vue.js laravel-elixir

对于需要使用变量的VueJS组件,我遇到了一个相当简单的问题。问题在于让sass在组件中注册变量。

我尝试导入包含我的变量的_variables.scss文件,但没有运气。在这一点上,非常感谢任何指导,或者如果组件有另一种方式继承样式。

MyComponent.vue

<template>
    <div class="my-color"></div>
</template>
<style lang="sass">
    .my-color {
        color: $primary-color;
    }
</style>
<script>
    export default{
        data(){
            return {}
        }
    }
</script>

Gulpfile.js

var elixir = require('laravel-elixir');
require('laravel-elixir-vueify');

elixir(function(mix) {
    mix.browserify('main.js');
    mix.sass('app.scss');
});

app.scss

@import "node_modules/bootstrap-sass/assets/stylesheets/bootstrap";
@import "modules/variables";

variables.scss

$primary-color: #BA2731; //Red

5 个答案:

答案 0 :(得分:14)

在每个组件中导入_variables.scss似乎是我迄今为止找到的唯一解决方案(it should work, according to this issue)。

<template>
    <div class="my-color"></div>
</template>
<style lang="sass">
    @import 'path/to/your/_variable.scss'; // Using this should get you the variables
    .my-color {
        color: $primary-color;
    }
</style>
<script>
    export default{
        data(){
            return {}
        }
    }
</script>

由于您只想包含变量,因此这应该不是问题。

但正如问题所述,请注意:您应该只在每个组件中包含抽象实体(变量,扩展,混合),而不是具体的CSS规则。

答案 1 :(得分:2)

我正在使用sass-loader的最新版本(v9.0.2),而prependData似乎现在不是一种选择。您可能想在vue-config.js中尝试此配置。请注意,由于不再使用additionalData选项,因此正在使用prependData选项。

module.exports = {
  css: {
    loaderOptions: {
      sass: {
        additionalData: '@import "@/pathto/variables.scss";'
      }
    }
  }
}

答案 2 :(得分:1)

对于我的项目,我使用了Vue CLI webpack,这是一个适合我的解决方案。

我管理App.vue文件中的所有SCSS。在我的每个Component.vue中,我停止使用<style></style>并开始创建单独的component.scss

所以我的src文件夹如下:

 /src
     /assets
     /components
         /compA
             CompA.vue
             compA.scss
         /compB
             CompB.vue
             compB.scss
    App.vue
    main.js

我的App.vue看起来像

<template> ... </template>

<script> ... </script>

<style lang="less">
    //Bootstrap
    @import "../node_modules/bootstrap-sass/assets/stylesheets/bootstrap";

    //Components
    @import "components/compA.scss";
    @import "components/compB.scss";
</style>

此设置的优点是可以在一个位置管理所有样式,并且还能够使用所有引导变量和mixin。

我走了这条路线,因为当我在所有组件中导入Boostrap SCSS时,我的应用程序的大小不断增长。如果我只导入变量,那么增加可以忽略不计,但是当我导入整个Boostrap SCSS时,增加变得很大。我这样做,所以我可以使用mixins并扩展一些现有的样式。

答案 3 :(得分:1)

假设您正在使用vue-cli,请尝试此操作。

如果尚未安装sass加载程序:

npm install -D sass-loader node-sass

然后在vue.config.js中输入

const path = require('path');

module.exports = {
  css: {
    loaderOptions: {
      sass: {
        data: `@import "@/pathto/variables.scss";`
      }
    }
  }
};

在您的组件中:

<style lang="sass">
    .my-color {
        color: $primary-color;
    }
</style>

来源:

Passing options to pre-processor loaders

How to import a sass file into every vue component

答案 4 :(得分:1)

对我来说,上述解决方案均无效(sass-loader 9.0.3),但这确实有效:

module.exports = {
  css: {
    loaderOptions: {
      sass: {
        sassOptions: {
          prependData: `@import "@/scss/variables.scss";`
        }
      },
    },
  },
};
相关问题