How do I access Sass variables inside a Vue component?
Asked Answered
D

3

12

I am using Vueify with Laravel/Elixir. I use Sass inside my Post.vue file but it references colors I declare in @import "resources/assets/sass/bootstrap-variables-override.scss"; Is there a way to not have to include that line inside of every single .vue component I make?

Delladelle answered 24/12, 2015 at 19:48 Comment(1)
I don't know of anyway with browserify. But with webpack you can simple use this loader: github.com/shakacode/sass-resources-loader and make sure to use <style lang="sass!sass-resources"> in your vue fileArmlet
F
6

There's a discussion on the official vue-loader repo with the exact same question and it boils down to: no, you have to import the variables file in each .vue component that needs it.

One thing you can do to simplify the situation slightly is to add the folder with the variables file to your include paths, so you can just do @import "my-variables.scss"; instead of specifying the entire path every time.

Formyl answered 14/1, 2017 at 15:53 Comment(0)
S
2

you can archive this by importing this in you top level component, usually called <app></app> and expose it by using a css class:

.primaryColor {
 color: <variable here>
}

Then you can just access that class in every child component

Salaidh answered 27/3, 2016 at 18:49 Comment(0)
H
0

Not always applicable but if you're using Vue as part of a Nuxt application then you can use style-resources-module to achieve this.

Hoe answered 20/12, 2019 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.