Can I pass parameters in computed properties in Vue.Js

Can I pass parameters in computed properties in Vue.Js

Asked on November 13, 2018 in vue.js.
Add Comment


  • 2 Answer(s)

    Most Commonly, we use a function:

    <span>{{ fullName('Hi') }}</span>
    methods: {
      fullName(salut) {
          return `${salut} ${this.firstName} ${this.lastName}`
      }
    }
    
    Answered on November 13, 2018.
    Add Comment

    As we know that, a parameter can be passed to calculated method, like that a parameter is passed to this method.

    Example,

    {
      itemById: function(state) {
        return (id) => state.itemPool[id];
      }
    }
    

    It can be recorded to the calculated method of an element:

    computed: {
      ...mapGetters([
        'ids',
        'itemById'
      ])
    }
    

    Then, try this calculated method as below:

    <div v-for="id in ids" :key="id">{{itemById(id).description}}</div>
    

    Just apply similar method to produce a calculated function which take parameter.

    computed: {
      ...mapGetters([
        'ids',
        'itemById'
      ]),
      descriptionById: function() {
        return (id) => this.itemById(id).description;
      }
    }
    

    Then practice it

    <div v-for="id in ids" :key="id">{{descriptionById(id)}}</div>
    
    Answered on November 13, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.