Part 1 sử dụng name=$event
C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\App.vue
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<hello-world/>
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'App',
components: {
HelloWorld
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\HelloWorld.vue
<template>
<div class="container">
<h1>{{ name }}</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"></hitest>
</div>
<div class="col-md-6">
<pitest></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"
}
},
components: {
Hitest,
Pitest
},
methods: {
changeName() {
this.name = "change Name";
}
}
}
</script>
C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\Hitest.vue
<template>
<div class="hello">
<h1>{{ name }}</h1>
<button @click="resetName">ResetName</button>
</div>
</template>
<script>
export default {
name: 'Hitest',
data: function() {
return {
name: "Piiii"
}
},
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">
Pitest
</div>
</template>
<script>
export default {
name: 'Pitest'
}
</script>
Part 2 sử dụng calbackfunction
C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\HelloWorld.vue
<template>
<div class="container">
<h1>{{ name }}</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"></hitest>
</div>
<div class="col-md-6">
<pitest></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"
}
},
components: {
Hitest,
Pitest
},
methods: {
changeName() {
this.name = "change Name";
},
resetNameCallback() {
this.name = 'resetNameCallback';
}
}
}
</script>
C:\Users\Administrator\OneDrive\Desktop\vuejs2\src\components\Hitest.vue
<template>
<div class="hello">
<h1>{{ name }}</h1>
<button @click="resetName">ResetName</button>
<button @click="resetNameFn">resetNameCallback</button>
</div>
</template>
<script>
export default {
name: 'Hitest',
data: function() {
return {
name: "Piiii",
}
},
props: {
resetNameFn: Function
},
methods: {
resetName() {
this.name = "Reseted";
this.$emit('resetNameParent',this.name);
}
}
}
</script>