Processing data inside watch()

Published 5 months ago by afoysal

My HTML code is like below

<ul>
   <li v-for="item in values">{{ item }}</li>
</ul>

My vue.js code is like below

export default {
        data() {
            return {
                values: []
            }
        },
        props: ['applicants', 'pageinfo'],
        watch: {
            applicants (val) {
                EventBus.$on('click',  function (skillName) {
                    this.values = val[0];   
        console.log(this.values);  // I am getting output here.
                });
            },
        },
    }

I am trying to iterate over the values of val[0]. But I am not getting any output.

bobbybouwmann

You need to use another method to assign the value, since you're not in the Vue scope anymore.

applicants (val) {
    var that = this;

    EventBus.$on('click',  function (skillName) {
        that.$set('values', val[0]);
    });
},

Documentation: https://vuejs.org/v2/api/#Vue-set

Please sign in or create an account to participate in this conversation.