uni-app h5 提示 uni is not defined
uni-app h5 提示 uni is not defined
4 回复
vue 2 里正常吗?
更多关于uni-app h5 提示 uni is not defined的实战教程也可以访问 https://www.itying.com/category-93-b0.html
是的
请问解决了吗?
问题分析:
uni is not defined 是 uni-app 在 H5 平台运行时常见的错误,通常由以下原因导致:
-
未正确引入 uni-app 框架:
- 在 H5 页面中,必须通过
<script>标签或模块化方式引入uni-app的核心库(如uni.webview.1.5.2.js)。 - 若使用 Vue CLI 等自定义构建,需确保
uni对象被全局注入。
- 在 H5 页面中,必须通过
-
代码执行时机问题:
- 在
created或mounted生命周期中直接调用uniAPI,但框架尚未初始化完成。 - 建议将
uniAPI 调用放在onReady或onLoad中,或使用setTimeout延迟执行。
- 在
-
多端兼容未处理:
- 部分 API 仅支持小程序或 App,在 H5 中需通过条件编译或判断
uni是否存在来规避。 - 示例:
if (typeof uni !== 'undefined') { uni.showToast({ title: '提示' }); }
- 部分 API 仅支持小程序或 App,在 H5 中需通过条件编译或判断
-
构建配置问题:
- 检查
manifest.json中 H5 配置的router模式是否为history或hash,错误配置可能导致资源加载异常。 - 确保
devServer配置正确,避免热更新时脚本加载失败。
- 检查
解决方案:
-
检查引入顺序:
- 在
index.html中确认uni相关脚本优先于业务代码加载。 - 若使用 npm 包,通过
import 'uni-app'显式引入。
- 在
-
延迟调用 API:
export default { onReady() { uni.navigateTo({ url: '/pages/index/index' }); } } -
启用条件编译:
// #ifdef H5 console.log('仅 H5 平台执行'); // #endif

