原始方法
App.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <template> <div id="app"> <Father :age="age" @setage="setAge"/> </div> </template>
<script> import Father from './components/Father.vue'
export default { data(){ return{ age:"18" } }, name: 'app', components: { Father }, methods:{ setAge(res){ this.age = res; } } } </script>
<style>
</style>
|
Father.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <template> <div class="hello"> {{age}} </div> </template>
<script> export default { name: 'Father', props:{ age:String }, methods:{
}, mounted(){ this.$emit("setage","123"); } } </script>
<style scoped>
</style>
|
使用.sync修饰符
这里注意我们的事件名称被换成了update:age
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| <template> <div class="hello"> {{age}} </div> </template>
<script> export default { name: 'Father', props:{ age:String }, methods:{
}, mounted(){ this.$emit("update:age","123"); } } </script>
<style scoped>
</style>
|