"dependencies": {
"@dcloudio/uni-app": "3.0.0-4020320240708001",
"@dcloudio/uni-app-harmony": "3.0.0-alpha-4020320240703001",
"@dcloudio/uni-app-plus": "3.0.0-4020320240708001",
"@dcloudio/uni-components": "3.0.0-4020320240708001",
"@dcloudio/uni-h5": "3.0.0-4020320240708001",
"@dcloudio/uni-mp-alipay": "3.0.0-4020320240708001",
"@dcloudio/uni-mp-baidu": "3.0.0-4020320240708001",
"@dcloudio/uni-mp-jd": "3.0.0-4020320240708001",
"@dcloudio/uni-mp-kuaishou": "3.0.0-4020320240708001",
"@dcloudio/uni-mp-lark": "3.0.0-4020320240708001",
"@dcloudio/uni-mp-qq": "3.0.0-4020320240708001",
"@dcloudio/uni-mp-toutiao": "3.0.0-4020320240708001",
"@dcloudio/uni-mp-weixin": "3.0.0-4020320240708001",
"@dcloudio/uni-mp-xhs": "3.0.0-4020320240708001",
"@dcloudio/uni-quickapp-webview": "3.0.0-4020320240708001",
"pinia": "^2.1.7",
"vue": "3.4.31",
}
- 发布:2024-07-12 16:42
- 更新:2024-07-15 10:19
- 阅读:180
产品分类: uniapp/小程序/微信
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: Windows 11 22H2
第三方开发者工具版本号: Nightly Build 1.06.2407092
基础库版本号: 3.5.0
项目创建方式: CLI
CLI版本号: 3.0.0-4020320240708001
示例代码:
操作步骤:
// 组件
<script setup lang="ts">
defineExpose({
getData(args: number) {
// TODO:获取数据
console.log(args);
},
})
</script>
// 页面
<template>
<child-component ref="child"/>
</template>
<script setup lang="ts">
const child = ref<InstanceType<typeof childComponent> | null>(null)
setTimeout(() => {
console.log(child.value);
}, 800);
</script>
// 组件
<script setup lang="ts">
defineExpose({
getData(args: number) {
// TODO:获取数据
console.log(args);
},
})
</script>
// 页面
<template>
<child-component ref="child"/>
</template>
<script setup lang="ts">
const child = ref<InstanceType<typeof childComponent> | null>(null)
setTimeout(() => {
console.log(child.value);
}, 800);
</script>
预期结果:
Proxy {__v_skip: true, getData: ƒ}
[[Handler]]: Object
[[Target]]: Proxy
[[Handler]]: Object
[[Target]]: Object
getData: ƒ getData(args)
__v_skip: true
__proto__: Object
[[IsRevoked]]: false
[[IsRevoked]]: false
Proxy {__v_skip: true, getData: ƒ}
[[Handler]]: Object
[[Target]]: Proxy
[[Handler]]: Object
[[Target]]: Object
getData: ƒ getData(args)
__v_skip: true
__proto__: Object
[[IsRevoked]]: false
[[IsRevoked]]: false
实际结果:
Proxy {0: Proxy}
[[Handler]]: MutableReactiveHandler2
[[Target]]: Array(1)
0: Proxy {__v_skip: true, getData: ƒ}
length: 1
nv_length: (...)
__proto__: Array(0)
[[IsRevoked]]: false
Proxy {0: Proxy}
[[Handler]]: MutableReactiveHandler2
[[Target]]: Array(1)
0: Proxy {__v_skip: true, getData: ƒ}
length: 1
nv_length: (...)
__proto__: Array(0)
[[IsRevoked]]: false
bug描述:
子组件通过defineExpose宏暴露方法,在父组件模板内通过ref获取其实例,输出childIns.value为Array类型而非Object
-
回复 zZZ1Ma: 那可能是你之前的版本低,你更新一下uniapp版本 https://uniapp.dcloud.net.cn/quickstart-cli.html#cliversion
2024-07-15 14:50
zZZ1Ma (作者)
预期是这样没错,但是我这儿需要child.value[0].getData()才能调用getData方法,child.value.getData()结果是undefined,然后向上排查发现child.value是Array而非Object
2024-07-14 13:15
zZZ1Ma (作者)
上文的“预期结果”是HBuilderX创建的项目,“实际结果”是VScode通过Cli创建的项目
2024-07-14 13:17