【问题标题】:Angular2 [routerLink] works, this.router.navigate seems to crash the appAngular2 [routerLink] 有效,this.router.navigate 似乎使应用程序崩溃
【发布时间】: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


【解决方案1】:

我找错地方了。我将函数调用(单击)绑定到

<a href="" (click)="goToComponent">LINK</a> 

元素,失败了。

切换到

<span (click)="goToComponent">LINK</span> 

元素解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 2016-12-30
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    相关资源
    最近更新 更多