swan.onPageNotFound
兼容处理。 解释: 监听小程序要打开的页面不存在事件。该事件与 App.onPageNotFound 的回调时机一致。
方法参数
Function callback 小程序要打开的页面不存在的事件回调函数。
callback返回参数说明
示例
扫码体验
请使用百度APP扫码
图片示例
代码示例 1 :在生命周期的onPageNotFound中使用
在开发者工具中预览效果
// app.jsApp({ onLaunch() { }, onPageNotFound(res) { console.log(res); swan.showModal({ title: '', content: JSON.stringify(res) }); // 页面不存在时,默认跳转到首页,如果是 tabbar 页面,请使用 swan.switchTab 进行跳转 swan.navigateTo({ url: '/home/home' }); }});代码示例 2:等同于示例一的另一种写法
在开发者工具中预览效果
// app.jsApp({ onLaunch() { swan.onPageNotFound(function(res) { console.log(res); swan.showModal({ title: '', content: JSON.stringify(res) }); // 页面不存在时,默认跳转到首页,如果是 tabbar 页面,请使用 swan.switchTab 进行跳转 swan.navigateTo({ url: '/home/home' }); }); }});代码示例 3:可根据开发者的业务逻辑调整用法
在开发者工具中预览效果
Page({ data: {}, onTap() { swan.onPageNotFound(function(res) { console.log(res); swan.showModal({ title: '', content: JSON.stringify(res) }); // 页面不存在时,默认跳转到首页,如果是 tabbar 页面,请使用 swan.switchTab 进行跳转 swan.navigateTo({ url: '/home/home', success: res => { swan.offPageNotFound(); }, fail: err => { console.log('navigateTo fail'); } }); }); swan.navigateTo({ url: '/path/to/otherPage' }); }});Bug & Tip
- swan.offPageNotFound取消监听,否则会出现监听多次的情况
