uni-app uni.preloadPage预加载的nvue页面内 uni.getSystemInfoSync 报错 e.$getAppWebview is not a function
uni-app uni.preloadPage预加载的nvue页面内 uni.getSystemInfoSync 报错 e.$getAppWebview is not a function
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Windows | 11 | HBuilderX |
操作步骤:
<template>
<view><button @click="ttttttt">打开预加载页</button></view>
</template>
<script setup>
import { onReady } from '@dcloudio/uni-app'
const ttttttt = () => uni.navigateTo({ url: '/pages/test/test' })
onReady(() => uni.preloadPage({ url: '/pages/test/test' }))
</script>
<template>
<view><button @click="ttttttt">测试</button></view>
</template>
<script setup>
const ttttttt = () => {
console.log('onShow1')
console.log(uni.getDeviceInfo())
console.log(uni.getSystemInfoSync())
console.log('onShow2')
}
</script>
预期结果:
15:38:00.099 onShow1 at pages/test/test.nvue:6
15:38:00.099 [Object] {"brand":"sunmi","deviceBrand":"sunmi","deviceModel":"K2_C","devicePixelRatio":1,"deviceId"...} at pages/test/test.nvue:7
15:38:00.122 [Object] {"SDKVersion":"","appId":"__UNI__158359C","appLanguage":"zh-Hans","appName":"nvue测试preloadP...} at pages/test/test.nvue:8
15:38:00.122 onShow2 at pages/test/test.nvue:9
实际结果:
15:36:01.657 onShow1 at pages/test/test.nvue:6
15:36:01.674 [Object] {"brand":"sunmi","deviceBrand":"sunmi","deviceModel":"K2_C","devicePixelRatio":1,"deviceId"...} at pages/test/test.nvue:7
15:36:01.688 [Vue warn]: Unhandled error during execution of native event handler
at <ButtononClick=fn<ttttttt>>
at <Test__pageId=3__pagePath="pages/test/test"__pageQuery={}>
15:36:01.688 TypeError: e.$getAppWebview is not a function
bug描述:
安卓端 被 uni.preloadPage 预加载的 nvue 页面内使用 uni.getSystemInfoSync api 报错 e.$getAppWebview is not a function
4.66 版本使用标准基座可复现
更多关于uni-app uni.preloadPage预加载的nvue页面内 uni.getSystemInfoSync 报错 e.$getAppWebview is not a function的实战教程也可以访问 https://www.itying.com/category-93-b0.html
只有使用 getSystemInfoSync 的时候会报这个错误?除了安卓端,其他端会报错吗?
更多关于uni-app uni.preloadPage预加载的nvue页面内 uni.getSystemInfoSync 报错 e.$getAppWebview is not a function的实战教程也可以访问 https://www.itying.com/category-93-b0.html
没有测试其他端, 不过我测试发现nvue里预加载的页面的 onLoad 内 getCurrentInstance() 会报错 ReferenceError: getCurrentInstance is not defined ,
这是我今天适配 预加载时碰到的, 基本都是原nvue页面正常工作. 仅仅执行了预加载, 这些函数就不存在, 或者干脆报错了
回复 retrocode: 建议结合 onShow 生命周期来判断 onReady、onLoad 中的代码是否执行
回复 DCloud_UNI_LXH: 你好, 我上传了复现项目, 安卓使用标准基座就可以复现.
回复 DCloud_UNI_LXH: 你好, 如果NVUE停止维护了, 这个bug不打算修复的话, 能否告知一下大致是什么类的问题, 会影响哪些系统API, 我们这边想办法规避, nvue不开源我们也没法自己定位问题,
回复 DCloud_UNI_LXH: 是这样的, 你可能没有理解我的意思, 我在楼下附件中上传了demo, 你实机跑一下就可以理解了, 主要问题就是, getSystemInfoSync 在预加载时的onload和onready 生命周期函数内是可以正常执行的,
但是真正打开页面后, 在页面内通过 onshow 或 手动点击按钮执行对应函数, 就会报错.
如果只是单纯的 onload时报错还好, 现在是只有onload时是好着的.
经测试, 的确是有问题的, 我上传了复现项目, 你们可以测试一下. 安卓使用标准基座就可以
这是一个已知的uni-app框架在预加载nvue页面时的兼容性问题。当使用uni.preloadPage预加载nvue页面时,页面虽然已经创建但尚未完全初始化,此时调用uni.getSystemInfoSync()会因Webview组件未就绪而抛出$getAppWebview is not a function错误。
解决方案:
- 延迟调用:在页面生命周期中稍后调用系统信息API
onLoad(() => {
setTimeout(() => {
const systemInfo = uni.getSystemInfoSync()
// 处理系统信息
}, 100)
})
- 使用异步版本:改用
uni.getSystemInfo()替代同步版本
const ttttttt = async () => {
console.log('onShow1')
console.log(uni.getDeviceInfo())
try {
const systemInfo = await uni.getSystemInfo()
console.log(systemInfo)
} catch (error) {
console.error('获取系统信息失败:', error)
}
console.log('onShow2')
}
- 条件判断:在调用前检查Webview可用性
const ttttttt = () => {
if (typeof getCurrentPages === 'function') {
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
if (currentPage && currentPage.$getAppWebview) {
console.log(uni.getSystemInfoSync())
}
}
}


