最新国产好看的视频,伊人天堂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)部的方法,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下

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)文章

最新評論

通州市| 上高县| 娱乐| 齐齐哈尔市| 图们市| 许昌县| 孟津县| 黑龙江省| 凉山| 石河子市| 万宁市| 高尔夫| 济源市| 中西区| 华坪县| 徐闻县| 新宾| 略阳县| 南安市| 长武县| 紫云| 峨边| 巴楚县| 玉门市| 永新县| 江油市| 绥江县| 板桥市| 行唐县| 称多县| 阜康市| 两当县| 汤阴县| 绥中县| 浙江省| 吕梁市| 乌鲁木齐市| 华蓥市| 剑阁县| 长春市| 梁河县|