我正在使用angular 2构建一个应用程序,我需要检查基本href并将其与我的 app.routes.ts 文件中的预定义父路径集匹配。
目前我在提供程序中创建了一个硬编码的parentRoutes = ['login','signup','admin','user','manage']数组,其使用方式如下:
export let customProvider: any = {
provide: APP_BASE_HREF,
useFactory: () => {
let tenant = window['_app_base'];
let parentRoutes = ['login', 'signup', 'admin', 'user', 'manage'];
if (tenant !== '/') {
if (parentRoutes.indexOf(tenant) > -1)
window['_app_base'] = '/notenant';
else
window['_app_base'] = tenant;
}
else
window['_app_base'] = '/default'; // get default tenant from API
return window['_app_base'];
}
}
我需要使用以下服务动态加载此parentRoutes数组:
for (let i = 0; i < this.router.config.length; i++) {
this.parentRoutes .push(this.router.config[i].path);
}
但这必须在app.module.ts中的customProvider(上面写的)中完成
我尝试调用服务,但似乎无法从app.module.ts文件中调用。此外还存在生命周期问题。在加载应用程序时,app.module.ts中的customProvider签入在定义this.routePath之前运行。