官方语法 (this.$refs['组件ref属性值'] as ComponentPublicInstance)?.$callMethod('方法名', ...args)
<-- 父页面 ->
<template>
<view>
<child ref="c1"></child>
</view>
</template>
<script lang="uts">
import child from "./child.uvue"
export default {
components:{child},
data() {
return {
title: 'Hello'
}
},
onLoad() {
(this.$refs['c1' ] as ComponentPublicInstance )?.$callMethod('test','1234')
},
methods: {
}
}
</script>
<style>
.logo {
height: 100px;
width: 100px;
margin: 100px auto 25px auto;
}
.title {
font-size: 18px;
color: #8f8f94;
text-align: center;
}
</style>
<-- 组件页面 ->
<template>
<view>
1
</view>
</template>
<script lang="uts">
export default {
name:"child",
data() {
return {
}
},
methods: {
test(str : string){
console.log("调用组件:"+str);
}
}
}
</script>
<style>
</style>
c***@163.com (作者)
运行 安卓 我只是学习 写写demo
2025-07-14 17:41
DCloud_UNI_JBB
回复 c***@163.com: 发下你的代码,我看看是你写的有问题还是框架这边有问题
2025-07-14 19:01