I have an issue with “history back” when navigate to same page

喜夏-厌秋 提交于 2019-12-08 04:26:41

问题


When I navigate to the same component (with nativescript angular), I can intercept the params change but when i tap the Android Back button, it doesn't return back to previous page.

constructor(private pageRoute: PageRoute) {
    super();
    this.pageRoute.activatedRoute
        .switchMap(activatedRoute => activatedRoute.params)
        .forEach((params) => { 
            this._groupId = params['id'];
            this.load(); // this method reload the list in the page.
        });
}

I navigate in the same page "group/:id" with different url "home" -> "group/1" -> "group/2" -> "group/3". If I click Android Back Button in "group/3", I return to "home".

Can I add the "new page" in the application history?

Thanks


回答1:


What you want to do is use a CustomRouteReuseStrategy

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot } from '@angular/router';
import { NSLocationStrategy } from 'nativescript-angular/router/ns-location-strategy';
import { NSRouteReuseStrategy } from 'nativescript-angular/router/ns-route-reuse-strategy';

@Injectable()
export class CustomRouteReuseStrategy extends NSRouteReuseStrategy {

     constructor(location: NSLocationStrategy) {
         super(location);
     }

     shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
         return false;
     }
}

and inside your AppModule you want to put this as a provider

import { NgModule, NgModuleFactoryLoader, NO_ERRORS_SCHEMA } from "@angular/core";
import { RouteReuseStrategy } from "@angular/router";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";

import { AppRoutingModule } from "./app-routing.module";
import { CustomRouteReuseStrategy } from "./custom-router-strategy";
import { AppComponent } from "./app.component";

@NgModule({
    bootstrap: [
        AppComponent
    ],
    imports: [
        NativeScriptModule,
        AppRoutingModule
    ],
    declarations: [
        AppComponent
    ],
    providers: [
        {
            provide: RouteReuseStrategy,
            useClass: CustomRouteReuseStrategy
        }
    ],
    schemas: [
        NO_ERRORS_SCHEMA
    ]
})
export class AppModule { }

here is a example in play.nativescript.org

https://play.nativescript.org/?template=play-ng&id=AspHuI

(I didn't make this, I am just passing on the info that I have learned.)

Also, if you only want certain pages to reuse the route strategy then you would need to make additional changes of code

 shouldReuseRoute(future: ActivatedRouteSnapshot, current: ActivatedRouteSnapshot): boolean {
// first use the global Reuse Strategy evaluation function,
// which will return true, when we are navigating from the same component to itself
let shouldReuse = super.shouldReuseRoute(future, current);

// then check if the noReuse flag is set to true
if (shouldReuse && current.data.noReuse) {
  // if true, then don't reuse this component
  shouldReuse = false;
}

and then you could pass noReuse as a route param so that you have an additional check beyond the default "shouldReuse"

Hope this helps!



来源:https://stackoverflow.com/questions/40821005/i-have-an-issue-with-history-back-when-navigate-to-same-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!