[Vuejs]-Passing data property as function argument in vue.js

0👍

Dynamic property names are supposed to be accessed with bracket notation:

showElements(...elementNames){
     elementNames.forEach(name => {
           this[name] = true
     })
}

The method is supposed to be used like:

<button @click="showElements('div1')">Show<button>

1👍

Seems like you have a syntax error. Instead of writing to your data object like this:

 data() {
      return {
           div1 = false,
           div2 = false
      }
  }

You should write it like this:

 data() {
      return {
           div1: false,
           div2: false
      }
  }

Make sure to only use syntax that fits an object in the data object. Then, you can call it like this:

 <button @click="showElements(div1)">Show<button>

One more thing, when accessing the data you don’t actually need to write $data. Simply write ‘this.name’ in order to access your data.

Leave a comment