<IForm :form="form" ref="IForm"></IForm>
const IForm = ref();
这里的IForm是undefined
- 发布:2021-01-18 14:53
- 更新:2022-04-28 11:18
- 阅读:755
产品分类: uniapp/小程序/微信
PC开发环境操作系统: Windows
PC开发环境操作系统版本号: 64
第三方开发者工具版本号: 1.03
基础库版本号: 2.14.3
项目创建方式: CLI
CLI版本号: 2.0.0
示例代码:
操作步骤:
<IForm :form="form" ref="IForm"></IForm>
const IForm = ref();
这里的IForm是undefined
<IForm :form="form" ref="IForm"></IForm>
const IForm = ref();
这里的IForm是undefined
预期结果:
IForm成功获取到实例
IForm成功获取到实例
实际结果:
IForm是undefined
IForm是undefined
bug描述:
小程序端,父组件获取不了子组件的方法:
官方的demo中应该是用同名ref来获取组件实例:
这篇文章: https://blog.csdn.net/qq_43327962/article/details/111302644
但是我父组件这边这样写:
<IForm :form="form" ref="IForm"></IForm>
const IForm = ref();
这里的IForm是undefined
小菜啊 - 业精于勤荒于嬉
试了,我这没出现问题,方便贴全一点代码么
-
seho20001123 (作者)
父组件:
<view class="button">
<i-button @click="submit" size="large" round block>普通按钮</i-button>
<i-button ref="IButton" customStyle="margin-top: 24rpx;" plain size="large" type="default" round>返回</i-button>
</view>
onMounted(() => {
const IButton = ref();
console.log(IButton); // value是undefiend
});
你把你的放出来吧,看看是不是子组件不一样
2021-01-21 10:24
小菜啊 - 业精于勤荒于嬉
<template>
<button @click="sendValue">send</button>
<my-button ref="son" ></my-button>
</template>
setup() {
// 如果ref初始值是一个空,可以用于接受一个实例
const son = ref()
const sendValue = () => {
// 可以拿到son组件实例,并调用其setup返回的所有信息
console.log(son.value)
}
2022了,还是获取不到,我现在都是HBuilder X 3.4.7.20220422这个版本了,
父组件
<view>
<u-upload
ref="front"
:file-list="fileList"
:action="action"
/>
<view class="btn" @click="submit">submit</view>
</view>
<script>
import { ref } from 'vue';
export default {
setup() {
const front = ref(null)
const action = ref('https://www.example.com')
const fileList = ref([])
const shopRegisterFormRef = ref(null)
const store = useStore()
function submit() {
console.log('front:>>', front.value)
}
return {
front,
action,
fileList,
submit
}
}
}
</script>
子组件
<u-upload/>
// 打印出来还是null
front:>> null
seho20001123 (作者)
我在setup某函数中写的,这个函数是我手动点击button触发的,点击button的时候组件肯定已经挂载了,你出现过这个问题么
2021-01-19 09:51
seho20001123 (作者)
我试了一下,在普通的vue3项目中,是可以调的,但是使用uniapp在小程序环境下,才会报上面的问题
2021-01-19 09:51
小菜啊
回复 seho20001123: 小程序和App平台不能引用 view 等内置组件
2021-01-19 15:43
seho20001123 (作者)
回复 小菜啊: view text等内置组件肯定可以引入啊,要不然怎么写呢
2021-01-19 20:44