首页 > 其他分享 >vue3 获取组件实例 $ref方法踩坑 getCurrentInstance()

vue3 获取组件实例 $ref方法踩坑 getCurrentInstance()

时间:2022-11-15 12:12:52浏览次数:46  
标签:label getCurrentInstance 获取 实例 vue3 ref

ps:

我使用的是element-plus框架,有一个需求是级联选择器,选择后需要获取最后的id和label一起传递给后端。

获取id比较简单在change事件中使用value.pop()就可以获得

但是获取label的时候有点痛苦,因为vue3获取实例不像vue2可以直接this.$ref获取

参考:https://blog.csdn.net/F_fengzilin/article/details/118789302

https://blog.csdn.net/m0_49016709/article/details/117077579

 

后来找到了getCurrentInstance方法

然后我直接使用

currentInstance.ctx.$refs['demandUnitNameUP'].getCheckedNodes()[0].data.label 发现是可以取到值的 发布到测试环境以后发现一直报错 后来查看资料发现currentInstance.ctx只在本地有效 然后更改方式为 const { proxy } = getCurrentInstance(); proxy.$refs['demandUnitNameUP'].getCheckedNodes()[0].data.label 这样就没问题了

标签:label,getCurrentInstance,获取,实例,vue3,ref
From: https://www.cnblogs.com/huchong-bk/p/16891993.html

相关文章