0

In VueJS, I found that it's not possible to pass arguments to a computed property from methods object. I'd would like to know the logic behind this.

Is there a way to pass arguments to computed properties from methods object.

Something like this:

HTML

<div id="app">

  <p>Computed Property width: {{ width }}</p>

  <p>Changed Width: {{changed}}</p>

  <button @click="changeWidth">Get new Width</button>
</div>

JS

new Vue({
  el: '#app',
  data: {
    msg: 'Test Message',
    changed: 0
  },

  computed: { 
    width: function(factor){
        return 20 * factor;
    }

  },
  methods: { 
    changeWidth: function(){
        this.changed += 20 * this.width(5);
    }
  }
})
appu
  • 471
  • 1
  • 8
  • 26
  • Computed properties don't accept parameters like that. Why wouldn't you just make that a method? – thanksd Oct 06 '17 at 14:47
  • This looks like a matter of trying to do something in a certain way, that it wasn't intended to be used. Can you re-phrase the problem as what you're trying to solve, instead asking how to solve it using `computed`? – Daniel Oct 06 '17 at 16:47
  • @appu You might want to take a look at this https://stackoverflow.com/questions/44350862/method-vs-computed-in-vue/44350932#44350932 – thanksd Oct 06 '17 at 18:08

0 Answers0