Routing to the root directory

I am trying to use a route Router.navigate. I followed the instructions for the letter, but when I go through the API, it reloads the root page.

In mine RootComponentI try to use

this._router.navigate (['ABC', 'Page1']); which should redirect me to application / abc / xyz

But if I directly visit application/abc/xyzthrough my browser, it works without problems

app.component.ts

import {Component} from "angular2/core";
import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS} from "angular2/router";
import {RootComponent} from "./components/root.component";
import {Page1Component} from "./components/page1.component";


@Component({
    selector: 'app',
    template: '<router-outlet></router-outlet>',
    directives: [ROUTER_DIRECTIVES],
    providers: [
        ROUTER_PROVIDERS
    ]
})

@RouteConfig([
    {
        path: '',
        name: 'Root',
        component: RootComponent,
        useAsDefault: true
    },
    {
        path: '/abc/...',
        name: 'ABC',
        component: ABCComponent
    }
])

export class AppComponent {
}

Abccomponent

@Component({
    selector: 'abc',
    template: '<router-outlet></router-outlet>',
    directives: [ROUTER_DIRECTIVES]
})

@RouteConfig([
    {
        path: '/xyz',
        name: 'Page1',
        component: Page1Component
    }
])
export class ABCComponent{

    constructor(private _router:Router){
    }

}

Page1Component

import {Component} from "angular2/core";
import {Router} from "angular2/router";

@Component({
    selector: 'page1',
    template: '<h1>Page1</h1>'
})

export class Page1Component{

    constructor(private _router:Router){
    }
}

What am I doing wrong?

EDIT

To explain this even simpler terms

                              Application (2 routes at root level)
                                  |                        |
           Default ("/") - Root Component              /abc/ ABC Component
                                                           |
                                                       /abc/xyz Page1 Component

What I'm trying to do is go to Page1 from the root component.

Decision

After the reverse enineering from S.alem plunkr, here is the solution

http://plnkr.co/edit/F1p6aQNJ7lREHCiVnKEP?p=preview

+4
3

( AppComponent). :

getMainRouter(router?: Router):Router {
  if (router.parent === null) {
    return router;
  }
  return this.getMainRouter(router.parent);
}

, RootComponent :

// imports...

@Component({
    selector: 'root',
    template: '<h1>Root Component</h1>'
})

export class RootComponent{

    private _mainRouter: Router;

    constructor(private _router:Router){
        this._mainRouter = this.getMainRouter(this._router);
    }

    routeToSomewhere():void {
        this._mainRouter.navigate(['./ABC', 'Page1']);
    }

    private getMainRouter(router?: Router):Router {
        if (router.parent === null) {
            return router;
        }
        return this.getMainRouter(router.parent);
    }
}

plunker. Hero, , AppComponent. , URL- .

+1

. 2 :

  • / - RootComponent
  • /abc/xyz - Page1Component . .

, . question.

+1

Plunger example

@Component({
    selector: 'page1',
    template: `<h1>Page1</h1>
    <button (click)="navigateRoot()">to root</button>
    `
})
export class Page1Component{

    constructor(private _router:Router){}

    navigateRoot() {
      this._router.navigate(['/Root']);
    }
}

Make sure your server supports HTML5 pushState or, alternatively, supports HashLocationStrategy

import {bootstrap} from 'angular2/platform/browser';
import {provide} from 'angular2/core';
import {AppComponent} from './app';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {LocationStrategy, HashLocationStrategy} from 'angular2/platform/common';

bootstrap(AppComponent, [ROUTER_PROVIDERS, // ROUTER_PROVIDERS can also be provided at the root component
    provide(LocationStrategy, {useClass: HashLocationStrategy}])
  .catch(err => console.error(err));
0
source

All Articles