gatapia
gatapia

Reputation: 3662

How to navigate to angular2 default route

I have declared a route as default using useAsDefault: true. I now want to use this route without its name (its for a reusable component). Example:

<a [routerLink]="['/']">Go to default route</a>

or

this.router.navigate(['/']);

Is this possible? I had a quick look at the router source but could not even find a way to query the default route.

Upvotes: 0

Views: 231

Answers (1)

Daniel Kucal
Daniel Kucal

Reputation: 9242

The answer is: No, it's not possible at this time.

But if you really need, it can be hacked like this:

export class AppComponent {
  constructor(_route:RouteRegistry) {
    setTimeout(() => { 
      // it will print default route's name
      console.log(this._capitalize(_route._rules.values().next().value.defaultRule.hash))}, 
    500);
  }

  private _capitalize(str:string){
    if (!str) return "";
    return str[0].toUpperCase() + str.slice(1, str.length);
  }
}

Upvotes: 1

Related Questions