Description
my.onPageNotFound
Listens for the event where a page that the Mini Program attempts to open does not exist.
This event is triggered only during Mini Program cold starts or hot starts. It does not handle failures of the Routing.
If no listener is registered with my.onPageNotFound, a page not found prompt page is displayed when the target page does not exist.
Input parameter
Function listener
The listener function for the page-not-found event.
Parameter
Object res
|
Attribute |
Type |
Description |
|
path |
String |
The path (code package path) of the nonexistent page. |
|
query |
Object |
The query parameters of the nonexistent page. |
|
isEntryPage |
Boolean |
Specifies whether this is the first page of the current startup. For example, if a user enters the Mini Program from a shared link, the first page is the sharing page configured by the developer. |
Sample code
my.onPageNotFound(Function listener)
//app.js
const handlePageNotFound = (res) => {
my.redirectTo({
url: 'pages/...'
}); // For a tabbar page, use my.switchTab.
};
my.onPageNotFound(handlePageNotFound)
App({
onLaunch() {
}
})
-
Page redirection can be performed in the callback, but it must be processed synchronously. Asynchronous processing (for example, using
setTimeout) is invalid. -
The redirect target in the callback must be a page whose resources are already loaded. If the target is a subpackage page or plugin page that has not been loaded, an error occurs at runtime and the redirection fails.