国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Vue $emit $refs子父組件間方法的調(diào)用實例

2024-05-06 15:30:10
字體:
供稿:網(wǎng)友

1、$emit

子組件調(diào)用父組件的方法并傳遞數(shù)據(jù)

注意:子組件標簽中的時間也不區(qū)分大小寫要用“-”隔開

子組件:

<template> <button @click="emitEvent">點擊我</button></template><script> export default { data() {  return {  msg: "我是子組件中的數(shù)據(jù)"  } }, methods: {  emitEvent(){  this.$emit('my-event', this.msg)  //通過按鈕的點擊事件觸發(fā)方法,然后用$emit觸發(fā)一個my-event的自定義方法,傳遞this.msg數(shù)據(jù)。  } } }</script>

父組件:

<template> <div id="app"> <child-a @my-event="getMyEvent"></child-a> <!--父組件中通過監(jiān)測my-event事件執(zhí)行一個方法,然后取到子組件中傳遞過來的值--> </div></template><script> import ChildA from './components/child.vue' export default { components: {  ChildA }, methods: {  getMyEvent(msg){   console.log('接收的數(shù)據(jù)--------->'+msg)//接收的數(shù)據(jù)--------->我是子組件中的數(shù)據(jù)  } } }</script>

2、$refs

父組件調(diào)用子組件的方法,可以傳遞數(shù)據(jù)

注意:子組件標簽中的時間也不區(qū)分大小寫要用“-”隔開

父組件:

<template> <div id="app"> <child-a ref="child"></child-a> <!--用ref給子組件起個名字--> <button @click="getMyEvent">點擊父組件</button> </div></template><script> import ChildA from './components/child.vue' export default { components: {  ChildA }, data() {  return {  msg: "我是父組件中的數(shù)據(jù)"  } }, methods: {  getMyEvent(){   this.$refs.child.emitEvent(this.msg);   //調(diào)用子組件的方法,child是上邊ref起的名字,emitEvent是子組件的方法。  } } }</script>

子組件:

<template> <button>點擊我</button></template><script> export default { methods: {  emitEvent(msg){  console.log('接收的數(shù)據(jù)--------->'+msg)//接收的數(shù)據(jù)--------->我是父組件中的數(shù)據(jù)  } } }</script>

以上這篇Vue $emit $refs子父組件間方法的調(diào)用實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持錯新站長站。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 区。| 天祝| 屏山县| 镇江市| 阆中市| 黑龙江省| 当涂县| 延津县| 日喀则市| 松原市| 新干县| 合肥市| 南岸区| 东乌珠穆沁旗| 澳门| 济阳县| 定州市| 大宁县| 平果县| 富锦市| 陇南市| 图木舒克市| 宣城市| 宿迁市| 衡东县| 昂仁县| 东乌珠穆沁旗| 甘孜县| 托里县| 开鲁县| 扶沟县| 天全县| 宽城| 明光市| 辉南县| 伊金霍洛旗| 积石山| 平谷区| 建湖县| 孝感市| 尼勒克县|