【发布时间】:2018-04-20 20:47:19
【问题描述】:
在我的路由模块中,我以这种方式传递数据。
const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent, data: { ShowTopBar: true, showSideBar: false} },
{ path: 'error', component: ErrorComponent, data: { ShowTopBar: true, showSideBar: false}}
];
export const AppRoutingModule: ModuleWithProviders = RouterModule.forRoot(routes);
为了使数据类型安全,我创建了一个 RouteData 类,该类将保存 ShowTopBar 和 ShowSideBar 值并通过构造函数对其进行初始化。
export class RouteData {
constructor(showTopbar: boolean, showSideBar: boolean) {
this.ShowSideBar = showSideBar;
this.ShowTopBar = showTopbar;
}
public ShowTopBar: boolean;
public ShowSideBar: boolean;
}
现在,我通过以下方式更改了 Routes 的声明:
const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent, data: new RouteData(false, false) },
{ path: 'error', component: ErrorComponent, data: new RouteData(true, false)}
];
编译时出现以下错误:
静态解析符号值时遇到错误。调用函数 'RouteData',不支持函数调用。考虑更换 引用导出函数的函数或 la mbda, 解析符号 AppRoutingModule
我的问题是我们如何以类型安全的方式将RouteData 传递给路由,以便我可以利用类型安全。
【问题讨论】:
标签: typescript routing angular2-routing