abu abu
abu abu

Reputation: 7062

Using PROPS inside mounted() in vue.js

I have props like below

props: ['applicants'],

I would like to use that props like below

methods : {
fetchapplicants() {
                let self = this;
                console.log(self.applicants.length);
            }
        },
mounted() {
     this.fetchapplicants();
}

I am getting output undefined

enter image description here

Upvotes: 4

Views: 7985

Answers (1)

samayo
samayo

Reputation: 16495

First you don't need to use self from the code I see, there is no need for that. So, just do this.

methods : {
    fetchapplicants() {
        console.log(this.applicants.length);
    }
},
mounted() {
    this.fetchapplicants();
}

Also, make sure to pass props as objects, this is the recommended way and it will help you define the type and default value of the passed prop

props: {
    applicants: {
      type: Array,
      required: false, 
      default: []
    }
}

Last, make sure you are passing an array to your component. I think that may be the problem, so wherever in your code, when you call your components make sure to even test it by passing a default array as:

<my-component :applicants="[1, 2, 3, 4, 5, 6, 7]"></my-component>

Upvotes: 2

Related Questions