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

詳解Vue3?父組件調(diào)用子組件方法($refs?在setup()、<script?setup>?中使用)

 更新時間:2022年08月01日 11:02:38   作者:卡爾特斯  
這篇文章主要介紹了Vue3?父組件調(diào)用子組件方法($refs?在setup()、<script?setup>?中使用),在 vue2 中 ref 被用來獲取對應(yīng)的子元素,然后調(diào)用子元素內(nèi)部的方法,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

vue2ref 被用來獲取對應(yīng)的子元素,然后調(diào)用子元素內(nèi)部的方法。

<template>
  <!-- 子組件 -->
  <TestComponent ref="TestComponent"></TestComponent>
</template>

<script>
// 導(dǎo)入子組件
import TestComponent from './TestComponent'
export default {
  components: {
    TestComponent
  },
  mounted () {
    // 調(diào)用子組件方法
    this.$refs.TestComponent.show()
  }
}
</script>

Vue3 setup () {} 中使用 ref,如何獲取到子元素,并調(diào)用方法:

<template>
  <!-- 子組件 -->
  <TestComponent ref="RefTestComponent"></TestComponent>
</template>

<script>
// 導(dǎo)入子組件
import TestComponent from './TestComponent'
import { ref } from 'vue'
import { nextTick } from 'process'
export default {
  components: {
    TestComponent
  },
  setup () {
    // 定義一個對象關(guān)聯(lián)上子組件的 ref 值(注意:這里的屬性名必須跟子組件定義的 ref 值一模一樣,否者會關(guān)聯(lián)失效)
    const RefTestComponent = ref(null)
    // 延遲使用,因為還沒有返回跟掛載
    nextTick(() => {
      RefTestComponent.value.show()
    })
    // 返回
    return {
      RefTestComponent
    }
  }
}
</script>

Vue3 <script setup> 中使用 ref,如何獲取到子元素,并調(diào)用方法

<template>
  <!-- 子組件 -->
  <TestComponent ref="RefTestComponent"></TestComponent>
</template>

<script setup>
// 導(dǎo)入子組件
import TestComponent from './TestComponent'
import { ref } from 'vue'
import { nextTick } from 'process'

// 定義一個對象關(guān)聯(lián)上子組件的 ref 值(注意:這里的屬性名必須跟子組件定義的 ref 值一模一樣,否者會關(guān)聯(lián)失效)
const RefTestComponent = ref(null)
// 延遲使用,因為還沒掛載
nextTick(() => {
  RefTestComponent.value.show()
})
</script>

到此這篇關(guān)于Vue3 父組件調(diào)用子組件方法($refs 在setup()、<script setup> 中使用)的文章就介紹到這了,更多相關(guān)Vue3 父組件調(diào)用子組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

木里| 射阳县| 盘山县| 襄城县| 云和县| 志丹县| 天峻县| 延长县| 汉川市| 丹江口市| 屏东县| 法库县| 北安市| 渑池县| 敦化市| 汉寿县| 株洲市| 武陟县| 高淳县| 石景山区| 屏边| 定州市| 永德县| 郑州市| 太谷县| 民乐县| 竹溪县| 安平县| 云浮市| 北宁市| 贵定县| 郎溪县| 津南区| 通化县| 彭山县| 洪洞县| 云阳县| 都昌县| 类乌齐县| 余姚市| 盐边县|