0

Probably this is TOO basic - but nowadays I'm all kinda noob. I'm studying VueJS and I'm stuck in one problem.

I have this <h3> and its style is binded to a Vue Object.

<h3 :style="headerStyleP2" class="p2 font-ranchers col-xs-6 col-sm-12">
   P2
</h3>

And here's the Vue object....

var notas = new Vue({
  el: '#notas',
  data: {
    obs1: "Digite a nota da P1",
    obs2: "",
    obs3: "",
    input1: "",
    input2: "",
    input3: "",
    inputStyle: {
      color: "white"
    },
    headerStyleP2: {
      color: this.input1 ? "blue" : "grey"
    },
    headerStyleP3: {
      color: "aqua"
    }
  }
})

So there you guys can see that I'm using a ternary. It's kinda working - but it's not being dynamic. When I refresh the page it's grey - but it never turns blue when I change the input1 value. (it's binded to another input - this one is working great.)

I'm felling I'm doing this the wrong way. How can I make it work?

1
  • Do a computed property for it please. Commented Sep 21, 2020 at 1:49

1 Answer 1

2

Make the headerStyleP2 to be a computed property instead so it will always recompute with the latest value of this.input1. More info about computed properties here.

<h3 :style="headerStyleP2" class="p2 font-ranchers col-xs-6 col-sm-12">...</h3>
computed: {
  headerStyleP2() {
    return { color: this.input1 ? "blue" : "grey" };
  }
}

enter image description here

Here's a demo.

Sign up to request clarification or add additional context in comments.

2 Comments

I'd like to say one thing, mister - please keep being this extremely helpful guy. It's wonderful that you even made a gif. Thank you so much. Worked beautifully.
You're welcome. Just doing what I can to help out a fellow coder. Goodluck!

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.