getCurrentPages
getCurrentPages()
is uesed to get the instance of the page stack, it will return an array of page. The first element is the home page, and the last element is the current page.
The framework maintains the current pages by stack. And the relationship of routing switch and page stack is shown in the following table.
Routing | Page Stack Behavior |
Initialization | Push new page into stack |
Open a new page | Push new page into stack |
Redirect of page | Pop current page from stack and push new page into stack |
Page returned | Pop current page from stack |
Tab switch | Pop all pages from stack except the new tab page |
Following codes can help to detect whether current stack reaches 5 layer of pages.
copy
if (getCurrentPages().length === 5) {
my.redirectTo({
url: '/pages/logs/logs'
});
} else {
my.navigateTo({
url: '/pages/index/index'
});
}
Note: do not try to modify the page stack, or error about page routing and page status may happen.
FAQ
Q: How to get the path of current page by getCurrentPages()
?
A: JSON.stringify(getCurrentPages()[0].__proto__.route)
can get the path of current page.