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.

RoutingPage Stack Behavior
InitializationPush new page into stack
Open a new pagePush new page into stack
Redirect of pagePop current page from stack and push new page into stack
Page returnedPop current page from stack
Tab switchPop 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.