我想知道是否可以以任何可能的方式在 Angular Route中使用 (Typescript) 泛型。
最好的方法是如果我可以对组件本身使用泛型:
// the route:
{
path: 'user-a',
component: UserComponent<UserA> // other routes will use UserB and UserC
}
// the component:
@Component({...})
export class UserComponent<T> { ... }
这显然给出了一个错误,但它很好地说明了我想要完成的事情。
另一种方法是将泛型与Resolver一起使用:
// the route:
{
path: '/user-b',
component: UserComponent,
resolve: { user: UserResolver<UserB> }
}
// the resolver:
export class UserResolver<T> implements Resolve<boolean> {}
这个答案使用了这种方法,但对我来说它给出了一个错误UserResolver<UserB>
:“'typeof UserResolver'类型的值不可调用。你的意思是包括'new'吗?”