Search code examples
angularnativescriptrouter

NativeScript & Angular - How to clear the router history?


I am working on a NativeScript 6 project with Angular 8.

I want to clear the router history after the user logs in.

This is what I've tried:

public onLogin() {

this.registerUserCredentialsService
    .registerUserCredentials(this.username, this.password)
    .then(() => {
        this.router.navigate(['home-page'], { clearHistory: true });
    });

}

I keep getting this error:

ERROR in src/app/register-user-credentials.component.ts(33,55): error TS2345: Argument of type '{ clearHistory: boolean; }' is not assignable to parameter of type 'NavigationExtras'.
  Object literal may only specify known properties, and 'clearHistory' does not exist in type 'NavigationExtras'.
How can I clear the router history?

Solution

  • NativeScript < 7.0

    Try using RouterExtensions instead of Router

    import { RouterExtensions } from "nativescript-angular/router";
    
    ...
    constructor(
      private routerExtensions: RouterExtensions
    ) {}
    ...
    this.routerExtensions.navigate(['home-page'], { clearHistory: true });
    

    NativeScript > 7.0

    RouterExtensions has a different import path in more recent NativeScript versions (it's usage is still the same)

    import { RouterExtensions } from "@nativescript/angular";
    
    ...
    constructor(
      private routerExtensions: RouterExtensions
    ) {}
    ...
    this.routerExtensions.navigate(['home-page'], { clearHistory: true });