mirror of
				https://github.com/ionic-team/ionic-framework.git
				synced 2025-11-04 21:30:00 +08:00 
			
		
		
		
	fix(router): wait RAF
This commit is contained in:
		@ -17,6 +17,7 @@ export class Router {
 | 
				
			|||||||
  private busy = false;
 | 
					  private busy = false;
 | 
				
			||||||
  private init = false;
 | 
					  private init = false;
 | 
				
			||||||
  private state = 0;
 | 
					  private state = 0;
 | 
				
			||||||
 | 
					  private lastState = 0;
 | 
				
			||||||
  private timer: any;
 | 
					  private timer: any;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Element() el: HTMLElement;
 | 
					  @Element() el: HTMLElement;
 | 
				
			||||||
@ -36,7 +37,13 @@ export class Router {
 | 
				
			|||||||
    const tree = readRoutes(this.el);
 | 
					    const tree = readRoutes(this.el);
 | 
				
			||||||
    this.routes = flattenRouterTree(tree);
 | 
					    this.routes = flattenRouterTree(tree);
 | 
				
			||||||
    this.redirects = readRedirects(this.el);
 | 
					    this.redirects = readRedirects(this.el);
 | 
				
			||||||
    this.writeNavStateRoot(this.getPath(), RouterDirection.None);
 | 
					
 | 
				
			||||||
 | 
					    this.historyDirection();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // TODO: use something else
 | 
				
			||||||
 | 
					    requestAnimationFrame(() => {
 | 
				
			||||||
 | 
					      this.writeNavStateRoot(this.getPath(), RouterDirection.None);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Listen('ionRouteRedirectChanged')
 | 
					  @Listen('ionRouteRedirectChanged')
 | 
				
			||||||
@ -71,17 +78,29 @@ export class Router {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  @Listen('window:popstate')
 | 
					  @Listen('window:popstate')
 | 
				
			||||||
  protected onPopState() {
 | 
					  protected onPopState() {
 | 
				
			||||||
 | 
					    const direction = this.historyDirection();
 | 
				
			||||||
 | 
					    const path = this.getPath();
 | 
				
			||||||
 | 
					    console.debug('[ion-router] URL changed -> update nav', path, direction);
 | 
				
			||||||
 | 
					    return this.writeNavStateRoot(path, direction);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private historyDirection() {
 | 
				
			||||||
    if (window.history.state === null) {
 | 
					    if (window.history.state === null) {
 | 
				
			||||||
      this.state++;
 | 
					      this.state++;
 | 
				
			||||||
      window.history.replaceState(this.state, document.title, document.location.href);
 | 
					      window.history.replaceState(this.state, document.title, document.location.href);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    const direction = window.history.state >= this.state
 | 
					 | 
				
			||||||
      ? RouterDirection.Forward
 | 
					 | 
				
			||||||
      : RouterDirection.Back;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const path = this.getPath();
 | 
					    const state = window.history.state;
 | 
				
			||||||
    console.debug('[ion-router] URL changed -> update nav', path, direction);
 | 
					    const lastState = this.lastState;
 | 
				
			||||||
    return this.writeNavStateRoot(path, direction);
 | 
					    this.lastState = state;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (state > lastState) {
 | 
				
			||||||
 | 
					      return RouterDirection.Forward;
 | 
				
			||||||
 | 
					    } else if (state < lastState) {
 | 
				
			||||||
 | 
					      return RouterDirection.Back;
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      return RouterDirection.None;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Method()
 | 
					  @Method()
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user