navigator.getPages() does not return the correct value when navigator.resetToPage()

Asked 1 years ago, Updated 1 years ago, 75 views

I am using onsen-ui navigator.I only want to show the Back button if there is a page stack, but navigator.getPages() does not seem to return the correct value within the prepush, postpush event from navigator.resetToPage().

navigator.on("prepush", function(e){
  var pages = navigator.getPages();
  console.log (pages.length); // 1
});

navigator.on("postpush", function(e){
  var pages = navigator.getPages();
  console.log(pages.length);//2
});

navigator.resetToPage("pages/hoge.html");

In prepush, postpush immediately after resetToPage, navigator.getPages() returns the same value as pushPage (prepush:1, postPush:2 in the code above).If you've saved three page stacks, prepush:3, postPush:4.

Since the page stack is cleared, I think both should return prePush:0 and postPush:1.After resetToPage, pushPage again returns the correct value after clearing (prepush:1, postPush:2).

Is there any way to avoid this phenomenon?If you set onTransitionEnd for resetToPage callback, it seems that the correct value will be returned, but I would like to use prePush and postPush.

monaca onsen-ui cordova

2022-09-30 16:13

2 Answers

The resetToPage implementation is as follows:

resetToPage:function(page, options){
    options=options||{};

    if(!options.animator&!options.animation){
      options.animation='none';
    }

    varonTransitionEnd=options.onTransitionEnd||function(){};
    varself = this;

    options.onTransitionEnd=function(){
      while(self.pages.length>1){
        self.pages.shift().destroy();
      }
      self._scope.$digest();
      onTransitionEnd();
    };

    This.pushPage(page, options);
  },

Instead of pushPage() after emptying the page stack, pushPage() completes the page stack. The function specified in onTransitionEnd causes questions after the page stack is discarded.
If you do not empty the page stack before pushPage(), I think you are assuming an animation transition with resetToPage() (although there is no proof).

Due to the above specifications, prepush, postpush cannot be used as of the current version 1.3.14. Use the onTransitionEnd option or
onsBackButton Let navigator.pages.length be $watch() as the directive does.


2022-09-30 16:13

navigator is declared in window, so
Why don't you call it window.navigator?

Also, it seems that they have decided whether to return after checking the number of pages.
If you just want to check if you can go back,
window.navigator.canPopPage();//true or false
...so, can't we?


2022-09-30 16:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.