【发布时间】:2017-08-02 00:43:03
【问题描述】:
我在 Angular2 应用程序中遇到路由问题,使用这样的按钮可以正常工作:
<button [routerLink]="['/home/maincomponent']>Click</button>
但是在函数中使用相同的路由失败 - 页面似乎加载了一瞬间,然后应用程序完全刷新并让我回到主/根页面,但控制台上没有错误。
goToComponent() {
this.router.navigate(['/home/maincomponent']);
}
此外,只需在浏览器中输入路径即可导航到该路径,可以正常加载组件,不会出现任何错误或重定向。
http://localhost:4000/#/home/component
路由器配置:
const routes: Routes = [
{
path: 'home',
component: HomeComponent,
children: [{
path: '',
redirectTo: 'dashboard',
pathMatch: 'full'
},
{
path: 'dashboard',
component: DashboardComponent
},
{
path: 'search',
component: SearchComponent
},
{
path: 'maincomponent',
component: MainComponent
},
{
path: '**',
component: DashboardComponent
}]
}
];
@NgModule 配置:
RouterModule.forRoot(routes,
{
useHash: true,
preloadingStrategy: PreloadAllModules
}),
【问题讨论】:
-
你能把你的路由配置设置一下吗。
-
我已将路由器配置添加到问题中。谢谢!
标签: angular routes routerlink