Sha*_*awn 5 angular2-routing angular
在RC1的新路由器中,无法再使用useAsDefault.相反,现在默认路由在app.component.ts中实现
ngOnInit() {
this.router.navigate(['/login']);
}
Run Code Online (Sandbox Code Playgroud)
如果我按浏览器上的" 重新加载"按钮刷新页面,那么我当前的页面网址http://localhost:3000/heroshell/heroes将会更改为,http://localhost:3000/login因为每次按下"重新加载"按钮,它都将通过app.component.ts
ngOnInit() {
this.router.navigate(['/login']);
}
Run Code Online (Sandbox Code Playgroud)
我的当前页面仍会显示,但出现错误.
browser_adapter.ts:78 EXCEPTION: Error in app/apps/hero/hero-shell.component.html:3:7
browser_adapter.ts:78 ORIGINAL EXCEPTION: TypeError: Cannot read property 'map' of null
Run Code Online (Sandbox Code Playgroud)
这是app/apps/hero/hero-shell.component.html
<my-app1>
<h1>{{title}}</h1>
<nav>
<a [routerLink]="['dashboard']">Dashboard</a>
<a [routerLink]="['heroes']">Heroes</a>
</nav>
<router-outlet></router-outlet>
</my-app1>
Run Code Online (Sandbox Code Playgroud)
所以我的问题是
Beta路由没有这种行为,因为不需要通过ngOnInit()来定义默认路由.
有关我的文件夹结构和路由设置的更多信息,请参阅定义但未识别的Angular2 RC1子路由
更新:如果我使用
{ path: '/', component: Login },
Run Code Online (Sandbox Code Playgroud)
配对
ngOnInit() {
this.router.navigate(['/']);
}
Run Code Online (Sandbox Code Playgroud)
然后,URL将更改为http://localhost:3000/命中重新加载按钮时错误保持不变.
同样,如果我使用上面的更新将路径更改为' ',那么当重新加载按钮被点击时,URL将被更改为' http:// localhost:3000 / '并且错误保持不变.
解决问题的关键是使用两件事的组合:
在app.component.ts中的@Routes中使用'/'或'*'定义根路径
{path:'*',component:Login},
摆脱ngOnInit()app.component.ts,以便它不会改变URL.
再次感谢大家的投入.干杯:)
在RC1中对我有用的是
{ path: '', component: FooComponent, index: true }
{ path: 'foo', component: FooComponent }
小智 4
抱歉,不明白第一个问题和第三个问题,但为此:
- 有没有办法在不通过 ngOnInit() 的情况下创建默认路由?
您是否尝试过将“*”作为默认路由?
{ path: '/heroshell', component: HeroShellComponent },
{ path: '/home', component: HomeComponent },
{ path: '/login', component: Login },
{ path: '*', component: Login },
Run Code Online (Sandbox Code Playgroud)
默认路由为“Login”,“/login”路径也可以使用。
| 归档时间: |
|
| 查看次数: |
4819 次 |
| 最近记录: |