3๐
โ
You can add immediate: true
to your watcher:
Vue.component('Child', {
template: `
<div class="">
{{ currentView }}
</div>
`,
props: {
view: {
type: String,
required: true,
},
},
watch: {
view: {
immediate: true,
handler(val) {
const viewOptions = {
people: {
array: this.peopleArray,
name: 'People world',
},
robots: {
array: this.robotArray,
name: 'Robot world',
},
};
this.currentView = viewOptions[val];
immediate: true
console.log(this.currentView.array) //when I console log this, I want to get the peopleArray or robotArray
},
},
},
data(){
return{
peopleArray: ['Sam', 'Rob'],
robotArray: ['Wall-E', 'R2D2'],
currentView: {}
}
},
})
new Vue({
el: '#demo',
data() {
return {
type: 'people'
}
},
methods: {
change() {
this.type === 'people' ? this.type = 'robots' : this.type = 'people'
console.log(this.type)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<button @click="change">change</button>
<child :view="type" />
</div>
๐คNikola Pavicevic
1๐
That property value depends on another value means this is the case for computed property:
computed: {
currentView() {
const viewOptions = {...};
return viewOptions[this.view];
}
...
๐คEstus Flask
Source:stackexchange.com