uni-app vue3获取组件实例为null
uni-app vue3获取组件实例为null
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Windows | win10 X64 | HBuilderX |
示例代码:
<template>
<view class="content">
<view class="color">{{num}}</view>
<button @btnClick="btnClick">按钮</button>
<uni-popup ref="popupRef" type="bottom">底部弹出 Popup</uni-popup>
</view>
</template>
<script setup>
import {ref, onMounted} from 'vue'
const popupRef = ref(null)
const num = ref(123)
onMounted(() => {
console.log('popupRef实例:', popupRef.value);
})
function btnClick() {
console.log(popupRef);
popupRef.value.open()
}
</script>
操作步骤:
- 点击按钮获取组件实例
预期结果:
- 可以获取到uni-popup组件实例
实际结果:
- 获取结果为null
bug描述:
官方的实例还是使用v2写法,当使用v3的setup语法就会获取不到组件实例
未复现此问题。请用HBuilder X 3.4.8-alpha试下
很容易复现啊,内置组件都是null
我也是获取内置组件的ref是null
<picker
ref=“pickerRef”
mode=“date”
:value=“state.date”
:start=“startDate”
:end=“endDate”
:fields=“state.fields”
@change=“bindDateChange”
>
<view class="uni-input">{{ state.date }}</view>
</picker>
在 uni-app
中使用 Vue 3
时,如果你尝试获取组件实例时得到 null
,可能是由于以下几个原因导致的。以下是一些常见的原因和解决方法:
1. 组件尚未挂载
在 Vue 3
中,组件的生命周期钩子 onMounted
会在组件挂载到 DOM 之后执行。如果你在 onMounted
之前尝试获取组件实例,可能会得到 null
。
解决方法:
确保在 onMounted
钩子中获取组件实例。
import { ref, onMounted } from 'vue';
export default {
setup() {
const myComponent = ref(null);
onMounted(() => {
console.log(myComponent.value); // 这里可以获取到组件实例
});
return {
myComponent,
};
},
};
2. 组件未正确引用
如果你在模板中使用了 ref
,但没有正确绑定到组件实例,也会导致获取到的实例为 null
。
解决方法:
确保在模板中正确使用 ref
。
<template>
<my-component ref="myComponent"></my-component>
</template>
3. 组件未正确注册
如果你尝试获取的组件未在父组件中正确注册,也会导致获取到的实例为 null
。
解决方法: 确保组件在父组件中正确注册。
import MyComponent from './MyComponent.vue';
export default {
components: {
MyComponent,
},
setup() {
const myComponent = ref(null);
onMounted(() => {
console.log(myComponent.value); // 这里可以获取到组件实例
});
return {
myComponent,
};
},
};
4. 异步组件
如果你使用的是异步组件,组件可能尚未加载完成,导致获取到的实例为 null
。
解决方法:
使用 onMounted
或 watch
来确保组件加载完成后再获取实例。
import { ref, onMounted } from 'vue';
export default {
setup() {
const myComponent = ref(null);
onMounted(() => {
if (myComponent.value) {
console.log(myComponent.value); // 这里可以获取到组件实例
}
});
return {
myComponent,
};
},
};
5. 组件销毁
如果你在组件销毁后尝试获取实例,也会得到 null
。
解决方法: 确保在组件销毁前获取实例。
import { ref, onBeforeUnmount } from 'vue';
export default {
setup() {
const myComponent = ref(null);
onBeforeUnmount(() => {
if (myComponent.value) {
console.log(myComponent.value); // 这里可以获取到组件实例
}
});
return {
myComponent,
};
},
};
6. ref
未正确绑定
如果你在 setup
函数中使用了 ref
,但没有在模板中正确绑定,也会导致获取到的实例为 null
。
解决方法:
确保在模板中正确绑定 ref
。
<template>
<my-component :ref="myComponent"></my-component>
</template>
7. ref
与 v-if
或 v-for
一起使用
如果你在 v-if
或 v-for
中使用 ref
,可能会导致 ref
的值在组件未渲染时为 null
。
解决方法:
使用 v-show
代替 v-if
,或者在 v-for
中使用 ref
时,确保组件已经渲染。
<template>
<my-component v-show="isVisible" ref="myComponent"></my-component>
</template>
8. ref
与 teleport
一起使用
如果你在 teleport
中使用 ref
,可能会导致 ref
的值为 null
,因为 teleport
会将组件渲染到 DOM 的其他位置。
解决方法:
在 teleport
中使用 ref
时,确保组件已经渲染到目标位置。
<template>
<teleport to="body">
<my-component ref="myComponent"></my-component>
</teleport>
</template>