I have a concept-related in VUE.
I'm little bit confused about how data is passed to the child vue files. (vue instances? not familiar with the term honestly)
In some cases, the data changes in the child vue files doesn't change the data of parents when the data is passed by 'props'.
On the other hand, the data changes in child vue files, actually changes the data of parents.
I just encountered the case 2. which is when the data is passed through eventbus.
Can I understand this nature as below?
"Data is passed as a reference only when the data is passed via eventbus"
while,
"Data is apssed to the child as a seperate copy when the data is passed via props and the changes in the child will not change the original data in the parent"
Can I see the Vue nature in the way? It would be very grateful if someone could give me an example that the changes in child don't affect the data in the parent.
Always feel very guilty everytime I ask Vue-related quesitons cause the code is super long different from just Javascript questions. Thank you in advance.
The example I just ran into is as below.
=main.js=
import Vue from 'vue'
import App from './App.vue'
export const serverBus = new Vue()
new Vue({
el: '#app',
render: h => h(App)
})
=Servers.vue=
<template>
<div class="col-xs-12 col-sm-6">
<ul class="list-group">
<Server v-for='server in serverinfo' :serverinfo='server' :key='server.id'></Server>
</ul>
</div>
</template>
<script>
import Server from './server.vue'
export default {
components: {
Server
},
data() {
return {
serverinfo: [
{id:1, servno:'#1', servstat:'normal'},
{id:2, servno:'#2', servstat:'critical'},
{id:3, servno:'#3', servstat:'unknown'},
{id:4, servno:'#4', servstat:'error'},
{id:5, servno:'#5', servstat:'excellent'}]
}
}
}
</script>
<style>
</style>
=server.vue=
<template>
<li class='list-group-item' @click='serverselected'>
Server {{serverinfo.servno}}
</li>
</template>
<script>
import {serverBus} from '../../main.js'
export default {
props:['serverinfo'],
methods:{
serverselected(){
serverBus.$emit('serverselected', this.serverinfo)
}
}
}
</script>
<style>
</style>
=ServerDetails.vue=
<template>
<div class="col-xs-12 col-sm-6">
<p v-if='!server'>Please Select a server</p>
<p v-else>Server {{server.servno}} is selected. Status:{{server.servstat}}</p>
<button class="btn btn-primary" @click='backtonormal'>Back to Normal</button>
</div>
</template>
<script>
import {serverBus} from '../../main.js'
export default{
methods:{
backtonormal(){
this.server.servstat='normal'
}
},
data(){
return{
server:null
}
},
created(){
serverBus.$on('serverselected',(server)=>{
this.server=server
})
}
}
</script>
<style>
</style>