Kir*_*how 2 router ecmascript-6 angular
我使用Angular 2,SystemJs和ES6(非TypeScript).
我想要什么?我想导航到与Route连接.我在做什么
// route for exam simple
export let routes = [
{path: '', name: 'home', component: HomeComponent, terminal: true},
{path: 'auth', name: 'auth', component: AuthComponent}
]
Run Code Online (Sandbox Code Playgroud)
如果我使用[routerLink],它运行良好.现在我想以编程方式使用这样的路由器
import { Component } from '@angular/core';
import { Router, ROUTER_DIRECTIVES } from '@angular/router';
@Component({
selector: 'tf-main',
directives: [ROUTER_DIRECTIVES],
template: '<p>home</p><router-outlet></router-outlet>'
})
export class HomeComponent {
static get parameters() {
return [[Router]]
}
constructor(router) {
this.router = router;
// This wrong way!!!! Help!!!!
this.router.navigateByUrl(['auth']);
}
}
Run Code Online (Sandbox Code Playgroud)
只是
this.router.navigate(['auth']);
Run Code Online (Sandbox Code Playgroud)
要么
this.router.navigate(['/auth']);
Run Code Online (Sandbox Code Playgroud)
(确保使用根路由auth)
使用这个构造函数怎么样?
constructor(
private router: Router){}
Run Code Online (Sandbox Code Playgroud)
然后
ngOnInit() {
this.router.navigate(['auth']);
}
Run Code Online (Sandbox Code Playgroud)
我认为您的错误消息意味着您应该正确初始化“路由器”。
| 归档时间: |
|
| 查看次数: |
13299 次 |
| 最近记录: |