发布环境就会报错 e.uni.hideTabBar is not a function uni-app
发布环境就会报错 e.uni.hideTabBar is not a function uni-app
开发环境 | 版本号 | 项目创建方式 |
---|---|---|
Windows | win10 | HBuilderX |
操作步骤:
- uniapp vue3 在 main.js 中配置了
app.config.globalProperties.uni = uni;
- 在 index.vue 页面中 view 元素中直接点击事件执行
uni.hideTabBar();
代码 - 开发环境可以正常执行
- 发布环境就会报错
e.uni.hideTabBar is not a function
预期结果:
- 发布环境应给也可以
实际结果:
- 发布环境不可以
bug描述:
- uniapp vue3 在 main.js 中配置了
app.config.globalProperties.uni = uni;
- 在 index.vue 页面中 view 元素中直接点击事件执行
uni.hideTabBar();
代码 - 开发环境可以正常执行
- 发布环境就会报错
e.uni.hideTabBar is not a function
更多关于发布环境就会报错 e.uni.hideTabBar is not a function uni-app的实战教程也可以访问 https://www.itying.com/category-93-b0.html
不加上面那个全局配置还会报错吗
更多关于发布环境就会报错 e.uni.hideTabBar is not a function uni-app的实战教程也可以访问 https://www.itying.com/category-93-b0.html
不加 报错 TypeError: Cannot read properties of undefined (reading ‘hideTabBar’)
加上全局配置 开发环境不报错 但是 发布环境会报错 e.uni.hideTabBar is not a function
试下在script里面写一个函数呢?不要在template写箭头函数调用uni的这个API
<view @tap="()=>{uni.hideTabBar();}">test</view>
我写了一个demo,打包为web,再运行,测试了showLoading,hideTabbar 多个api 都没有报错
项目hx创建的空白vue3项目,没有修改main.js
// index.vue
<template>
<view @click="() => {
uni.showLoading({
title: ‘测试’,
mask: true
})
}">127272727</view>
</template>

这个问题的原因是发布环境下编译时对uni API的访问方式发生了变化。在Vue3中,直接通过globalProperties挂载uni对象可能会导致发布环境无法正确识别API。
解决方案有以下两种:
- 直接使用全局uni对象,无需挂载到globalProperties:
// 直接调用
uni.hideTabBar();
- 如果确实需要通过组件实例访问,可以这样修改:
// main.js中
app.config.globalProperties.$uni = uni;
// 组件中使用
this.$uni.hideTabBar();