欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3中使用ref標(biāo)簽對組件進(jìn)行操作方法

 更新時間:2023年04月17日 14:50:55   作者:knva  
這篇文章主要介紹了Vue3中使用ref標(biāo)簽對組件進(jìn)行操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

Vue3使用ref標(biāo)簽

在Vue2中 一般用 this.$ref.xxxx 進(jìn)行獲取組件對象
Vue3中就不使用這個方法了
例如:

 <el-upload class="upload-demo" action="" :http-request="handleUpload" 
                    :on-change="handleChange"
          :before-upload="handleBeforeUpload" :show-file-list="false" :auto-upload="false" :limit="1" ref="uploadRef">
          <el-button type="primary" icon="upload" slot="trigger">導(dǎo)入</el-button>
        </el-upload>

想要獲取el-upload組件對象

先創(chuàng)建

const uploadRef =  ref()

使用的話需要xxx.value.xxx
例如:

 // 清除上傳列表
  uploadRef.value.clearFiles()

補充:Vue3 中 ref 標(biāo)記組件使用

在 Vue3 中我們還可以使用 ref 標(biāo)記組件來進(jìn)行獲取父子組件獲取屬性和方法的操作。

父組件

<template>
<hello-world ref='son'></hello-world>
<button @click='getSon()'>獲取</button>
</template>

<script setup>
// 首先還是先引入子組件
import HelloWorld from './components/HelloWorld.vue'

// 然后引入 ref ,并聲明 son
import {ref} from 'vue'
const son = ref()
const getSon = () => {
console.log(son.value.title)
son.value.sonMethod()
}

</script>

子組件

<template>
  <div> {{ title }} </div>
</template>

<script setup>
import {ref} from 'vue'
const title = ref('我是子組件的title')
const sonMethod = () => {
  console.log('我是子組件的方法')
}

// 最要的一步,這里我們需要把父組件需要的屬性和方法暴露出去,這樣父組件才能獲取的到
defineExpose({sonMethod, title})
</script>

到此這篇關(guān)于Vue3中 如何使用ref標(biāo)簽,對組件進(jìn)行操作的文章就介紹到這了,更多相關(guān)Vue3使用ref標(biāo)簽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論