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

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

 更新時(shí)間:2021年09月14日 09:03:25   作者:貓老板的豆  
這篇文章主要介紹了Vue子組件調(diào)用父組件方法案例詳解,本篇文章通過簡要的案例,講解了該項(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('測試');
		}
	}
};
</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('測試');
		}
	}
};
</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('測試');
		}
	}
};
</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)論

定边县| 神农架林区| 肇东市| 三门峡市| 巩义市| 万源市| 来宾市| 兰溪市| 临清市| 吉木乃县| 荥经县| 泾川县| 茶陵县| 旬邑县| 濮阳县| 河北区| 齐齐哈尔市| 望江县| 巫溪县| 姚安县| 东乌| 霍山县| 鄯善县| 大同市| 舞阳县| 蒙城县| 广灵县| 永宁县| 巴青县| 桂林市| 通海县| 临澧县| 沈丘县| 禄劝| 广河县| 河东区| 合江县| 勃利县| 海原县| 黄石市| 留坝县|