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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With