When defining my routes in the routing module, I have structured the data passing like this:
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);
To ensure type safety for the data being passed, I created a class called RouteData
. This class holds the values of ShowTopBar
and ShowSideBar
, which are initialized through its constructor.
export class RouteData {
constructor(showTopbar: boolean, showSideBar: boolean) {
this.ShowSideBar = showSideBar;
this.ShowTopBar = showTopbar;
}
public ShowTopBar: boolean;
public ShowSideBar: boolean;
}
I then modified the route declarations as follows:
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)}
];
However, upon compiling, I encountered the following error:
Error encountered resolving symbol values statically. Calling function 'RouteData', function calls are not supported. Consider replacing the function or lambda with a reference to an exported function, resolving symbol AppRoutingModule
My concern is how can I pass RouteData
in a type-safe way to Routes so that I can leverage type-safety effectively.