Vue子組件調(diào)用父組件事件的3種方法實(shí)例
更新時(shí)間:2024年01月11日 11:43:09 作者:嗯哼姐
大家在做vue開(kāi)發(fā)過(guò)程中經(jīng)常遇到父組件需要調(diào)用子組件方法或者子組件需要調(diào)用父組件的方法的情況,這篇文章主要給大家介紹了關(guān)于Vue子組件調(diào)用父組件事件的3種方法,需要的朋友可以參考下
1. 在子組件中通過(guò)this.$parent.event來(lái)調(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)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- vue 父組件獲取子組件里面的data數(shù)據(jù)(實(shí)現(xiàn)步驟)
- Vue子組件如何修改父組件數(shù)據(jù)的方法及注意事項(xiàng)
- vue3父子組件通信之子組件修改父組件傳過(guò)來(lái)的值
- Vue3子組件向父組件傳值的兩種實(shí)現(xiàn)方式
- vue父組件值變化但子組件不刷新的三種解決方案
- vue中父組件通過(guò)props向子組件傳遞數(shù)據(jù)但子組件接收不到解決辦法
- vue子組件實(shí)時(shí)獲取父組件的數(shù)據(jù)實(shí)現(xiàn)
- 關(guān)于VUE點(diǎn)擊父組件按鈕跳轉(zhuǎn)到子組件的問(wèn)題及解決方案
相關(guān)文章
vue pages 多入口項(xiàng)目 + chainWebpack 全局引用縮寫(xiě)說(shuō)明
這篇文章主要介紹了vue pages 多入口項(xiàng)目 + chainWebpack 全局引用縮寫(xiě)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09vue?cesium加載點(diǎn)與定位到指定位置的實(shí)現(xiàn)方法
Cesium是一個(gè)用于創(chuàng)建高性能、跨平臺(tái)的3D地球和地圖的開(kāi)源JavaScript庫(kù),它提供了許多功能,包括地理空間數(shù)據(jù)可視化、地理定位和地圖導(dǎo)航等,這篇文章主要介紹了vue?cesium加載點(diǎn)與定位到指定位置,需要的朋友可以參考下2024-03-03vue報(bào)錯(cuò)"vue-cli-service‘不是內(nèi)部或外部命令,也不是...”的解決辦法
這篇文章主要介紹了vue報(bào)錯(cuò)"vue-cli-service‘不是內(nèi)部或外部命令,也不是...”的解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01vue?element-plus圖片預(yù)覽實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue?element-plus圖片預(yù)覽實(shí)現(xiàn)的相關(guān)資料,最近的項(xiàng)目中有圖片預(yù)覽的場(chǎng)景,也是踩了一些坑,在這里總結(jié)一下,需要的朋友可以參考下2023-07-07