最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue子組件調(diào)用父組件方法案例詳解

 更新時(shí)間:2021年09月14日 09:03:25   作者:貓老板的豆  
這篇文章主要介紹了Vue子組件調(diào)用父組件方法案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下

一、直接在子組件中通過this.$parent.event來調(diào)用父組件的方法

<!-- 父組件 -->
<template>
	<div>
		<child></child>
	</div>
</template>
<script>
import child from '~/components/dam/child';
export default {
	components: {
		child
	},
	methods: {
		fatherMethod () {
			console.log('測(cè)試');
		}
	}
};
</script>
<!-- 子組件 -->
<template>
  <div>
    <button @click="childMethod()">點(diǎn)擊</button>
  </div>
</template>
<script>
  export default {
    methods: {
      childMethod() {
        this.$parent.fatherMethod();
      }
    }
  };
</script>

二、在子組件里用$emit向父組件觸發(fā)一個(gè)事件,父組件監(jiān)聽這個(gè)事件

<!-- 父組件 -->
<template>
	<div>
		<child @fatherMethod="fatherMethod"></child>
	</div>
</template>
<script>
import child from '~/components/dam/child';
export default {
	components: {
		child
	},
	methods: {
		fatherMethod () {
			console.log('測(cè)試');
		}
	}
};
</script>
<!-- 子組件 -->
<template>
	<div>
		<button @click="childMethod()">點(diǎn)擊</button>
	</div>
</template>
<script>
export default {
	methods: {
		childMethod () {
			this.$emit('fatherMethod');
		}
	}
};
</script>

三、父組件把方法傳入子組件中,在子組件里直接調(diào)用這個(gè)方法

<!-- 父組件 -->
<template>
	<div>
		<child :fatherMethod="fatherMethod"></child>
	</div>
</template>
<script>
import child from '~/components/dam/child';
export default {
	components: {
		child
	},
	methods: {
		fatherMethod () {
			console.log('測(cè)試');
		}
	}
};
</script>
<!-- 子組件 -->
<template>
	<div>
		<button @click="childMethod()">點(diǎn)擊</button>
	</div>
</template>
<script>
export default {
	props: {
		fatherMethod: {
			type: Function,
			default: null
		}
	},
	methods: {
		childMethod () {
			if (this.fatherMethod) {
				this.fatherMethod();
			}
		}
	}
};
</script>

到此這篇關(guān)于Vue子組件調(diào)用父組件方法案例詳解的文章就介紹到這了,更多相關(guān)Vue子組件調(diào)用父組件方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

宁乡县| 顺平县| 万载县| 安义县| 若尔盖县| 巴彦淖尔市| 嘉兴市| 陆河县| 松潘县| 南丹县| 兴仁县| 县级市| 鹤壁市| 洛宁县| 达拉特旗| 辽源市| 台湾省| 筠连县| 衡山县| 二连浩特市| 巫溪县| 嵊州市| 志丹县| 沾化县| 莎车县| 浦城县| 措勤县| 五大连池市| 电白县| 高阳县| 赤水市| 娄烦县| 顺昌县| 孟村| 永兴县| 清水河县| 大竹县| 墨竹工卡县| 金乡县| 高州市| 麻阳|