Vue子組件調(diào)用父組件事件的3種方法實(shí)例
1. 在子組件中通過this.$parent.event來調(diào)用父組件的方法,data參數(shù)可選
<template> <div> <h1>我是父組件</h1> <child /> </div> </template> <script> import child from '@/components/child'; export default { components: { child }, methods: { fatherMethod(data) { console.log('我是父組件方法'); } } }; </script>
<template> <div> <h1>我是子組件</h1> <button @click="childMethod(data)">點(diǎn)擊</button> </div> </template> <script> export default { methods: { childMethod() { this.$parent.fatherMethod(data); console.log('調(diào)用父組件方法') } } }; </script>
2.父組件使用v-bind綁定一個(gè)變量(v-bind:變量名="值"),子組件用props接收(與created同級(jí))
<template> <div> 這是父組件 <child :parentHandler="parentHandler" /> </div> </template> <script> import child from "@/components/child"; export default { components: { child }, data() { return {}; }, methods: { parentHandler() { console.log("這是父組件的方法"); }, }, }; </script>
<template> <div> 這是子組件 <button @click="handler">這是子組件的按鈕</button> </div> </template> <script> export default { props: { parentHandler: { type: Function, default: () => { return Function; }, }, //parentHandler: { // type: Object, // default: () => { // return {} // }, //}, // parentHandler: { // type: Boolean, // default: true, // }, // parentHandler: { // type: Array, // default: () => { // return [] // }, // }, // parentHandler: { // type: String, // default: '', // }, }, methods: { handler() { this.parentHandler(); }, }, }; </script>
3.使用$refs傳值
<template> <div> <h3>這是父組件</h3> <button @click="setChildInfo">向子組件傳值</button> <h3>這是父組件中引用的子組件</h3> <child ref="child"></child> </div> </template> <script> //子組件地址(僅供參考),具體以實(shí)際項(xiàng)目目錄地址為準(zhǔn) import child from "./Child.vue"; export default { components: { child: child }, data() { return {}; }, methods: { // 向子組件傳值 setChildInfo() { this.$refs.child.cInfo = "c2"; //this.$refs.child.open("c2"); } } }; </script>
<template> <div> <p>收到父組件數(shù)據(jù):{{ cInfo }}</p> </div> </template> <script> export default { data() { return { cInfo: "c1" }; }, methods: { //open(data) { // console.log(data); //}, }, }; </script>
總結(jié)
到此這篇關(guān)于Vue子組件調(diào)用父組件事件的3種方法的文章就介紹到這了,更多相關(guān)Vue子組件調(diào)用父組件事件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)折疊展開收縮動(dòng)畫效果
這篇文章主要介紹了vue實(shí)現(xiàn)折疊展開收縮動(dòng)畫,通過scrollHeight實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2023-11-11vue項(xiàng)目實(shí)現(xiàn)中英文切換的詳細(xì)步驟
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目實(shí)現(xiàn)中英文切換的詳細(xì)步驟,項(xiàng)目中經(jīng)常有中英文切換的功能,接下來就簡(jiǎn)單實(shí)現(xiàn)以下這個(gè)功能,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考2023-11-11Vue實(shí)現(xiàn)視頻播放vue-video-player、dplayer方式
這篇文章主要介紹了Vue實(shí)現(xiàn)視頻播放vue-video-player、dplayer方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04vue設(shè)置導(dǎo)航欄、側(cè)邊欄為公共頁(yè)面的例子
今天小編就為大家分享一篇vue設(shè)置導(dǎo)航欄、側(cè)邊欄為公共頁(yè)面的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11el-form resetFields無(wú)效和validate無(wú)效的可能原因及解決方法
本文主要介紹了el-form resetFields無(wú)效和validate無(wú)效的可能原因及解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08vue+vant使用圖片預(yù)覽功能ImagePreview的問題解決
這篇文章主要介紹了vue+vant使用圖片預(yù)覽功能ImagePreview的問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Vue組件實(shí)現(xiàn)數(shù)字滾動(dòng)抽獎(jiǎng)效果
這篇文章主要為大家詳細(xì)介紹了Vue組件實(shí)現(xiàn)數(shù)字滾動(dòng)抽獎(jiǎng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03解決vue中使用swiper插件問題及swiper在vue中的用法
Swiper是純javascript打造的滑動(dòng)特效插件,面向手機(jī)、平板電腦等移動(dòng)終端。這篇文章主要介紹了解決vue中使用swiper插件及swiper在vue中的用法,需要的朋友可以參考下2018-04-04