Use props inside method in vue.js

Published 5 months ago by afoysal

I have props like below

props: ['applicants',],

I would like to use props like below

  methods : {
            formated (item) {
                var _self = this;                
                _self.values.length = 0;
                if(item == "") {
                   _self.values = this.applicants //I am getting output here
                }
                else {
            this.applicants.filter(applicant => { }) //I am not getting output here
                }
            }
   }

Why it is happening like this?

wilk_randall

Just wondering why you're assigning _self to this? You typically don't need to do that in your methods. Also, I think you're not getting output from your else because filter does not mutate the array, you need to still re-assign the value. Your method could be re-written to something like this:

Also, what is values? Do you have that in your data on the component?


methods: {
    formated (item) {
        this.values.length = 0;

        if (item == "") {
            this.values = this.applicants;
        } else {
            this.values = this.applicants.filter(applicant => someConditionHere);
        }
    }
}

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