Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue js 2: Access props in mounted function

Tags:

I have data props in child components. Inside child component on mounted function I need to get specific value from props and set select dropdown value. I am using vue-multiselect plugin which is working fine. Here is the code.

module.exports = {     props: ["Subscriptions"]     mounted: function () {         let vm = this;                 Vue.nextTick(function () {               // I want to access props here but it return 0 length array              console.log(vm.$parent.Subscriptions);         });     },       beforeUpdate() {         let vm = this;         console.log(vm.$parent.Subscriptions);     }, //  updated() { //      let vm = this; //      console.log(vm.$parent.Subscriptions); //  } }; 

Now only time I do get subscriptions is in beforeUpdate and updated function but this called each time there is a change in value which is not required. I only need to change it first time in order to set drop down initial value.

like image 792
Epistemologist Avatar asked Jul 24 '17 05:07

Epistemologist


People also ask

How do you access props in Mount Vue?

To access props in mounted function with Vue. js, we add v-if to our component to show the component only when the prop is set. to render ChildComponent only if the testData reactive property is set.

How do you pass props in Vue 2?

To specify the type of prop you want to use in Vue, you will use an object instead of an array. You'll use the name of the property as the key of each property, and the type as the value. If the type of the data passed does not match the prop type, Vue sends an alert (in development mode) in the console with a warning.

Are Props passed by reference Vue?

Vue does not take a copy when you pass objects via props. If you pass an object via a prop then both components will be seeing the same object. As it's the same object, any property changes made by the child will also impact the other parent.

Can you pass functions as props in Vue?

You can pass strings, arrays, numbers, and objects as props. But can you pass a function as a prop? While you can pass a function as a prop, this is almost always a bad idea. Instead, there is probably a feature of Vue that is designed exactly to solve your problem.


1 Answers

A common mistake could be, parent component pass a variable as a prop which is null at the moment where child component get rendered. Thus when you access it in mounted in child component. You see it as a null value. And at a later time, in Parent component the variable which was passed (prop) will be assign the values from an asynchronous operation. To avoid possible pitfalls, better to use v-if.

Example :

<ChildComponent :data="testData" /> 

instead of above below can be used

<ChildComponent v-if="testData" :data="testData" /> 

so that only when testData available child component will be rendered. But if in child component you have some more things to show until data comes in better to use another component. And adding watchers will also resolve the problem but not in a pretty way.

Since you get values in updated hook probably this could be the case.

like image 119
dilantha111 Avatar answered Oct 09 '22 11:10

dilantha111