目录
@H_
777_7@
<template>
<div >
<h2>{{msg}}</h2>
<!-- 通过父组件给子组件传递函数类型的数据PRops实现:子给父传递数据 -->
<School :getName="getName"/>
<Student :getStudentname="getStudentname"/>
<!-- 通过父组件给子组件绑定一个自定义事件:实现子给父传递数据 -->
<Age v-on:elderSex="demo"/>
<!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第二种写法:使用ref) -->
<!-- <Student ref="student"/> -->
</div>
</template>
<script>
import School From './components/School.vue'
import Student from './components/Student.vue'
import Age from './components/Age.vue'
export default {
name:'App',
components:{ School,Student ,Age},
data(){
return {
msg:'你好,世界!'
}
},
methods:{
getName(name){
console.LOG('App收到了名字',name);
},
getStudentname(name1){
console.log('接收到了学生的姓名',name1);
},
demo(sex1){
console.log( 'demo被调用了',sex1);
}
},
// mounted() {
//绑定自定义事件
// this.$refs.student.$on('elderSex',this.schoolAge)
//绑定自定义事件(一次性)
// this.$refs.student.$once('elderSex',this.schoolAge)
// },
}
</script>
<style scoPEd>
</style>
<template>
<div class="demo">
<h2>学生姓名:{{name}}</h2>
<h2>学生年龄:{{age}}</h2>
<button @click="sendStudentname">把学生的名字给APP</button>
</div>
</template>
<script>
export default {
name: 'Student',
props: ['getStudentname'],
data() {
return {
name: '张三',
age: 19
}
},
methods: {
sendStudentname() {
this.getStudentname(this.name)
}
}
}
</script>
<style>
.demo {
background-color: skyblue;
}
</style>
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本宝典的更多内容!
脚本宝典总结
以上是脚本宝典为你收集整理的Vue之组件的自定义事件详解全部内容,希望文章能够帮你解决Vue之组件的自定义事件详解所遇到的问题。
如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。