Angular 2+路由redirectTo作为一个函数

问题描述 投票:2回答:1

我在Angular应用程序中有以下路由配置:

{
  path: ParentComponent.path,
  component: ParentComponent,
  canActivate: [AuthGuard],
  children: [
    {
      path: '',
      pathMatch: 'full',
      redirectTo: getDefaultChildRoute()
    },
    {
      path: ':mode/:year/:month/:day',
      component: ChildComponent
    }
  ]
}

getDefaultChildRoute()函数返回如下字符串:

export function getDefaultChildRoute(): string {
  const urlArray = ['a', '2019', '02'];
  return urlArray.join('/');
}

问题似乎是redirectTo需要一个字符串,但不是一个方法来调用(即使它返回一个字符串)。如果我用占位符字符串替换该方法调用,它可以正常工作。

在github已经存在一个问题:https://github.com/angular/angular/issues/13373

直接在redirectTo上使用箭头函数抛出Typescript错误'()=>字符串不能分配给字符串'。

有任何想法吗?

javascript angular typescript angular2-routing
1个回答
0
投票

试试这个:

redirectTo: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
    const urlArray = ['a', '2019', '02'];
    return urlArray.join('/');
}
© www.soinside.com 2019 - 2024. All rights reserved.