我正在尝试使用Router.navigate
方法进行路由。我按照说明书写了这封信,但是当我通过API路由时,它重新加载了根页。
在RootComponent
我试图使用
this._router.navigate(['ABC','Page1']);应该将我重定向到application / abc / xyz
但是,如果我通过浏览器直接访问application/abc/xyz
,它就能无缝地工作
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){
}
}
我做错了什么?
修改
用更简单的术语解释它
Application (2 routes at root level)
| |
Default ("/") - Root Component /abc/ ABC Component
|
/abc/xyz Page1 Component
我要做的是,从Root Component导航到Page1。
解决方案
从S.alem plunkr反向工程后,这是解决方案
答案 0 :(得分:1)
实际上路由器正在遵循您的配置。您已配置了2个路径:
0.05f
- 已映射到/
RootComponent
- 已映射到/abc/xyz
这些组件被认为是在同一层次结构上。所以组件在导航过程中互相替换。如果要使用嵌套组件,则需要使用子路由器和非终端路由。请检查此question。
答案 1 :(得分:1)
尝试使用主路由器(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 Tutorial,但是如果您使用AppComponent
的路径配置,则可以看到相关代码。在单独的窗口上启动plunker以查看浏览器URL。
答案 2 :(得分:0)
@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']);
}
}
确保您的服务器支持HTML5 pushState或者启用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));