Vue之組件的自定義事件詳解

<template>
<div >
<h2>{{msg}}</h2>
<!-- 通過父組件給子組件傳遞函數(shù)類型的數(shù)據(jù)props實(shí)現(xiàn):子給父傳遞數(shù)據(jù) -->
<School :getName="getName"/>
<Student :getStudentname="getStudentname"/>
<!-- 通過父組件給子組件綁定一個自定義事件:實(shí)現(xiàn)子給父傳遞數(shù)據(jù) -->
<Age v-on:elderSex="demo"/>
<!-- 通過父組件給子組件綁定一個自定義事件實(shí)現(xiàn):子給父傳遞數(shù)據(jù)(第二種寫法:使用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('接收到了學(xué)生的姓名',name1);
},
demo(sex1){
console.log( 'demo被調(diào)用了',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>學(xué)生姓名:{{name}}</h2>
<h2>學(xué)生年齡:{{age}}</h2>
<button @click="sendStudentname">把學(xué)生的名字給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>
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue 實(shí)現(xiàn)數(shù)字滾動增加效果的實(shí)例代碼
最近做了個項(xiàng)目需要做數(shù)字滾動增加的效果,剛開始接到這個項(xiàng)目還真是懵了,后來發(fā)現(xiàn)實(shí)現(xiàn)代碼很簡單的,下面小編給大家?guī)砹藇ue 實(shí)現(xiàn)數(shù)字滾動增加效果的實(shí)例代碼,需要的朋友參考下吧2018-07-07
Vue+ElementUI技巧之自定義表單項(xiàng)label的文字提示方法
這篇文章主要給大家介紹了關(guān)于Vue+ElementUI技巧之自定義表單項(xiàng)label文字提示的相關(guān)資料,文中通過圖文以及代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-02-02
Vue3生命周期Hooks原理與調(diào)度器Scheduler關(guān)系
這篇文章主要為大家介紹了Vue3生命周期Hooks原理與調(diào)度器Scheduler關(guān)系詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Vue Element 分組+多選+可搜索Select選擇器實(shí)現(xiàn)示例
這篇文章主要介紹了Vue Element 分組+多選+可搜索Select選擇器實(shí)現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
vue項(xiàng)目實(shí)現(xiàn)設(shè)置根據(jù)路由高亮對應(yīng)的菜單項(xiàng)操作
這篇文章主要介紹了vue項(xiàng)目實(shí)現(xiàn)設(shè)置根據(jù)路由高亮對應(yīng)的菜單項(xiàng)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
基于Vite2.x的Vue 3.x項(xiàng)目的搭建實(shí)現(xiàn)
這篇文章主要介紹了基于Vite2.x的Vue 3.x項(xiàng)目的搭建實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

