2

Is it possible for a Vue component to use inside part of the component a JavaScript value defined in part of the component?

For example:

<script>
export default {
  props: {
    value: Boolean
  }
};
</script>

<style lang="scss" scoped>
@if (value) {
  // Do something
}
</style>

2
  • Sass doesn't work like that, it's doesn't know about JavaScript. Commented Jun 21, 2018 at 13:51
  • @EmileBergeron I included Sass in my example, but it doesn't have to be Sass. Could be PostCSS for example. Commented Jun 21, 2018 at 15:24

3 Answers 3

5

No. Vue single file components and SASS preprocessing do not change the fundamental given: css are static. Your script will run in the browser, after your SASS has been resolved during the build step.

Most of the time, you get the dynamism you need by manipulating the classes on an element with vue, but there are some occasions where you want to directly manage the styles. For example, you might want to manage the position of an object with Vue. In this case, use a dynamic style binding ( which works and looks just like a dynamic class binding). These styles are injected directly onto the element, so they have precedence over all styles defined via css.

<div :style='myDynamicStyle'>...</div>

computed:{
    myDynamicStyle(){
        return {
            // define your style, in javascript, in camelCase
            position:absolute,
            top: this.myReactiveVar
            // when myReactiveVar changes, your thang will move baby
            ...
Sign up to request clarification or add additional context in comments.

2 Comments

Good note about the static compilation, though I think class binding (over inline style binding you've got here) is more appropriate to op's question as he specifically shows a style block and is asking about that.
@lix Yup. 99% of the time you'll have static styles in the style section, and reference them with class names which can be dynamic if needs be. But you can directly manage element style in Vue, and it's very handy for positioning. You're going to lose all the precedence and inheritance rules of css though, so you should only use this if you need it.
3

No, it's not possible, but you can achieve it almost in the same way using your property as main element's class. Like this:

<template>
  <div :class="myProp">
    <h1>Content 1</h1>
    <h2>Content 2</h2>
  </div>
</template>

<script>
  export default {
    props: {
      myProp: String
    }
  };
</script>

<style lang="scss" scoped>
  .propValue {
    & h1 {
      /* my rules here */
    }
    & h2 {
      /* my rules here */
    }
  }
</style>

1 Comment

I know about setting values ass class values, but what I actually needed is a conditional @import inside a stylesheet.
2

In Vue the preferred method for interacting between your data and styles is by using class bindings Here's the docs:

The main reason for using class bindings is because of Separation of Concerns both our data and style sections should only be concerened with talking to the template and introducing ways in which we can share data between the two would allow you to make further mistakes. It's also not a very efficent way to managing styles which are only concerned with the template data, so why create crossover?

example:

Template

<div class="static"
     v-bind:class="{ active: value }">
</div>

data

export default {
  props: {
    value: Boolean
  }
};

style

<style lang="scss" scoped>
  .active{
     color: green;
  }
</style>

As you can see here both of our separate components are directly relying on only the template to apply our custom styling.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.