Part 1: Truyền dữ liệu components con cùng cấp sử dụng $emit, hoặc callbackfunction
C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\HelloWorld.vue
<template>
<div class="container">
<h1>{{ name }}</h1>
<h1>{{ age }}</h1>
<p>
For a guide and recipes on how to configure / customize this project,<br>
check out the
<a href="https://cli.vuejs.org" target="_blank" rel="noopener">vue-cli documentation</a>.
</p>
<button @click="changeName">changeName</button>
<hr>
<div class="row">
<div class="col-md-6">
<hitest @resetNameParent="name=$event" :resetNameFn="resetNameCallback" :myAge="age" :myAgeFn="changeAge"></hitest>
</div>
<div class="col-md-6">
<pitest :myAge="age" :myAgeFn="changeAge" @editAgeParent="age=$event"></pitest>
</div>
</div>
</div>
</template>
<script>
import Hitest from "./Hitest.vue";
import Pitest from "./Pitest.vue";
export default {
name: 'HelloWorld',
data: function() {
return {
name: "Skype",
age: 18
}
},
components: {
Hitest,
Pitest
},
methods: {
changeName() {
this.name = "change Name";
},
resetNameCallback() {
this.name = 'resetNameCallback';
},
changeAge() {
this.age = 34;
}
}
}
</script>
C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\Hitest.vue
<template>
<div class="hello">
<h1>{{ name }}</h1>
<h2>{{myAge}}</h2>
<button @click="resetName">ResetName</button>
<button @click="resetNameFn">resetNameCallback</button>
</div>
</template>
<script>
export default {
name: 'Hitest',
data: function() {
return {
name: "Hiiii"
}
},
props: {
resetNameFn: Function,
myAge: Number,
},
methods: {
resetName() {
this.name = "Reseted";
this.$emit('resetNameParent',this.name);
}
}
}
</script>
C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\Pitest.vue
<template>
<div class="hello">
<h1>{{ name }}</h1>
<h2>{{myAge}}</h2>
<button @click="myAgeEmit">Change Age by myAgeEmit</button>
<button @click="myAgeFn">Change Age</button>
</div>
</template>
<script>
export default {
name: 'Pitest',
data: function() {
return {
name: "Piiii"
}
},
props: {
myAge: Number,
myAgeFn: Function
},
methods: {
myAgeEmit() {
this.myAge = 33;
this.$emit('editAgeParent',this.myAge);
}
}
}
</script>
Part 2: Sử dụng eventBus
C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\main.js
import Vue from 'vue'
import Hello from './components/HelloWorld.vue'
import App from './App.vue'
Vue.config.productionTip = false
export const eventBus = new Vue();
Vue.component('test',Hello)
new Vue({
render: h => h(App),
}).$mount('#app')
C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\Hitest.vue
<template>
<div class="hello">
<h1>{{ name }}</h1>
<h2>{{myAge}}</h2>
<button @click="resetName">ResetName</button>
<button @click="resetNameFn">resetNameCallback</button>
</div>
</template>
<script>
import {eventBus} from '../main.js';
export default {
name: 'Hitest',
data: function() {
return {
name: "Hiiii"
}
},
props: {
resetNameFn: Function,
myAge: Number,
},
methods: {
resetName() {
this.name = "Reseted";
// this.$emit('resetNameParent',this.name);
eventBus.$emit('resetNameParent',this.name);
}
},
created() {
eventBus.$on('editAgeParent',(age)=> {
this.myAge = age;
})
}
}
</script>
C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\Pitest.vue
<template>
<div class="hello">
<h1>{{ name }}</h1>
<h2>{{myAge}}</h2>
<button @click="myAgeEmit">Change Age by myAgeEmit</button>
<button @click="myAgeFn">Change Age</button>
</div>
</template>
<script>
import {eventBus} from '../main.js';
export default {
name: 'Pitest',
data: function() {
return {
name: "Piiii"
}
},
props: {
myAge: Number,
myAgeFn: Function
},
methods: {
myAgeEmit() {
this.myAge = 33;
// this.$emit('editAgeParent',this.myAge);
eventBus.$emit('editAgeParent',this.myAge);
}
}
}
</script>