当前位置: 首页> 娱乐> 八卦 > 莱芜网络公司案例_贵阳建设网站_查找网站_网络营销模式案例

莱芜网络公司案例_贵阳建设网站_查找网站_网络营销模式案例

时间:2025/7/12 4:28:49来源:https://blog.csdn.net/lotpyve/article/details/143907456 浏览次数:0次
莱芜网络公司案例_贵阳建设网站_查找网站_网络营销模式案例

例子:

我在使用vue2开发的时候,定义了一个方法

handler(strRef){ this.$refs[strRef].innerText = 'hello world' },

我在点击某个按钮的时候,调用了方法handler,传递了一个参数是字符串 'condition',然后方法中通过 this.$refs[strRef] 后去到绑定 ref = 'condition' 的组件,我在vue3中也需要通过这样的方法获取到相关组件

我们可以通过

import { getCurrentInstance } from 'vue'const instance: any = getCurrentInstance()

获取到当前的组件实例,其中 instance 就是当前组件实例,

组件代码:

<script setup lang="ts">
import { ref, onMounted, getCurrentInstance } from 'vue'const instance: any = getCurrentInstance()const xxx = ref('hello world')
const xxxInstance = ref(null)onMounted(() => {console.log(instance, 'instance')
})
</script>
<template><h1 ref="xxxInstance">{{ xxx }}</h1>
</template><style lang="less" scoped></style>

下面我们来输出一下,看看 instance 下面都有什么

可以看到 instance 下面存在 refs 对象,其中存在的就是绑定的各个组件实例

所以当 strRef 是字符串变量时,我们可以通过 instance.refs[strRef] 的方法获取到相关的组件实例

关键字:莱芜网络公司案例_贵阳建设网站_查找网站_网络营销模式案例

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: