dcloudio / uni-app

A cross-platform framework using Vue.js
https://uniapp.dcloud.io
Apache License 2.0
39.9k stars 3.62k forks source link

getCurrentInstance()获取refs得到的vue2写法的组件ref对象是空值,无法调用组件ref对象方法 #4053

Open Silentdoer opened 1 year ago

Silentdoer commented 1 year ago

问题描述 [问题描述:尽可能简洁清晰地把问题描述清楚] 用vk-uview的u-form的setRules方法可以测试 复现步骤 [复现问题的步骤]

  1. 启动 '...'
  2. 点击 '....'
  3. 查看

[或者可以直接贴源代码]

预期结果 [使用简洁清晰的语言描述你希望生效的预期结果]

实际结果 [这里请贴上你的报错截图或文字]

系统信息:

补充信息 [可选] [根据你的分析,出现这个问题的原因可能在哪里?]

StrivingRabbit commented 1 year ago

贴一下使用代码

Silentdoer commented 1 year ago

这种写法(setRules是uForm ref对象组件内部的methods方法)

const theRefs = getCurrentInstance()?.refs;
(theRefs?.uForm as any).setRules({})

const uForm = ref();
uForm.value.setRules({});

const theRefs = getCurrentInstance()?.proxy?.$refs;
(theRefs?.uForm as any).setRules({})

都试过了,都是报Cannot read property 'setRules' of undefined

Silentdoer commented 1 year ago

你好,可以看下吗?我用h5的vue3项目,const uForm = ref() 然后在onMounted里面打印uForm.value是有值的,但是在uni-app里不行,用的最新的uni-app cli创建的项目

zhetengbiji commented 1 year ago

贴一下完整点的代码:包含组件模版与options

zhangxinyong12 commented 1 year ago

遇到同样的问题。微信小程序vue3 获取不到ref实例。打印是null image

DougLee commented 1 year ago

遇到了同样的问题,有解决方法了吗

pkc918 commented 10 months ago

我也遇到了同样的问题,解决了嘛,老哥们