0

I have a JSFiddle below to explain my problem but basically I have an array called tiles which has a title. When the instance is created() I add a field to this array called active

I then output this array in an <li> element and loop through it outputting the title and active objects. My problem is as you can see in the fiddle when I run v-on:click="tile.active = true" nothing happens to the active state written in the <li> element

but if I run v-on:click="tile.title = 'test'" it seems to update the active object and the title object.

Its strange behaviour I can't seem to work out why. Does anyone have any ideas?

https://jsfiddle.net/jgb34dqo/

Thanks

2

1 Answer 1

1

It's to do with Vue not knowing about your properties, change your array to this:

tiles: [
  {
    title: 'tile one',
    active: false
  },
  {
    title: 'tile two',
    active: false
  },
  {
    title: 'tile three',
    active: false
  }
]

This allows Vue to know about the active property and in turn it knows to monitor that variable.

It's worth looking at this link about Vue Reactivity as it helps with understanding how and when data will change automagically.

If you must add the properties after

take a look at $set. It allows you to add props to an object that then get watched by vue. See this fiddle, notice the change:

this.tiles.forEach(function(tile) {
  // Tell vue to add and monitor an `active` prop against the tile object
  this.$set(tile, 'active',  false);
}.bind(this))
Sign up to request clarification or add additional context in comments.

3 Comments

I understand that my question is how do I make it understand my properties? I am pulling data from an API which doesn't have an active object so I have added this manually.
Ok, I understand, give me a moment, I'll fiddle an example for you.
Edit: Just fixed the fiddle link.

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.