vue3获取ref实例ts怎么写

文章讨论了在Vue组件中,父组件如何通过模板引用调用子组件的方法。通过使用`ref`和`defineExpose`,可以解决子组件方法在父组件中访问时的报错问题。但`defineExpose`可能会遇到eslint警告,解决方法是更新.eslintrc.js配置以允许使用`defineExpose`。
摘要由CSDN通过智能技术生成

父级件:

<template>
  <MyLogin ref="MyLoginRef" />
  <div @click="hanldClick">按钮</div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import MyLogin from './components/MyLogin.vue'

const MyLoginRef = ref<InstanceType<typeof MyLogin> | null>()
const hanldClick = () => {
  MyLoginRef.value?.sayHello()
}
</script>

<style scoped></style>

子级件MyLogin.vue:

<template>
  <div>我是login</div>
</template>

<script setup lang="ts">
defineExpose({
  sayHello: () => {
    console.log('sayHello........')
  }
})
</script>
<style scoped></style>

其中defineExpose可能会报错:‘defineExpose’ is not defined.eslint

Vue <script setup> compiler macro for declaring a component's exposed instance properties when it is accessed by a parent component via template refs.

<script setup> components are closed by default - i.e. variables inside the <script setup> scope is not exposed to parent unless explicitly exposed via defineExpose.

This is only usable inside <script setup>, is compiled away in the output and should not be actually called at runtime.

处理方法:.eslintrc.js
image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值