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

vue3組合式API獲取子組件屬性和方法的代碼實(shí)例

 更新時(shí)間:2024年01月30日 11:41:32   作者:椒鹽大肥貓  
這篇文章主要為大家詳細(xì)介紹了vue3組合式API獲取子組件屬性和方法的代碼實(shí)例,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

在vue2中,獲取子組件實(shí)例的方法或者屬性時(shí),父組件直接通過ref即可直接獲取子組件的屬性和方法,如下:

// father.vue
<child ref="instanceRef" />
this.$ref['instanceRef'].testVal
this.$ref['instanceRef'].testFunc()
// child.vue
data () {
	return {
		testVal: '來自子組件的屬性'
	}
},
methods: {
	testFunc() {
		return '來自子組件的方法'
	}
}

在vue3 組合式API中,在子組件使用defineExpose指定需要暴露的屬性和方法,父組件才可以通過ref獲取到子組件的屬性和方法,如下:

// father.vue
<script setup lang="ts">
import ChildInstance from "@/views/component/father-instance/child-instance.vue";
import { ref } from "vue";

const instanceRef = ref(null);
const getChildInstance = () => {
  const childInstance = instanceRef.value; // 通過ref獲取子組件實(shí)例
  console.log(childInstance.childValue);
  console.log(childInstance.childFunc());
};
</script>

<template>
  <ChildInstance ref="instanceRef" />
  <el-button @click="getChildInstance">獲取子組件屬性和方法</el-button>
</template>

<style scoped lang="scss"></style>

// child.vue
<script setup lang="ts">
import { ref, defineExpose } from "vue";

const childValue = ref("來自子組件的屬性");
const childFunc = () => {
  return "來自子組件的方法";
};
// 使用defineExpose指定需要暴露的屬性和方法
defineExpose({
  childValue,
  childFunc
});
</script>

<template>
  <div>來自子組件</div>
</template>

<style scoped lang="scss"></style>

以上就是vue3組合式API獲取子組件屬性和方法的代碼實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于vue3 API獲取子組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

莒南县| 鹤壁市| 承德县| 定陶县| 武胜县| 开封市| 兴隆县| 仁布县| 清远市| 岱山县| 隆林| 永春县| 缙云县| 凤冈县| 普兰店市| 江达县| 娄底市| 宜兰县| 大新县| 定边县| 礼泉县| 霍州市| 鹤岗市| 海阳市| 孝昌县| 水城县| 得荣县| 平山县| 普兰店市| 泾阳县| 沾益县| 阿克| 黄石市| 白山市| 昆山市| 杭锦旗| 琼结县| 长乐市| 稷山县| 田林县| 藁城市|