我正在尝试检索APP_INITIALIZER中网址中存在的数据
app.module.ts
export function init(config: ConfigService, router: Router) {
return () => config.load(router);
}
providers : [
...
{
provide: APP_INITIALIZER,
useFactory: init,
deps: [ConfigService, Router],
multi: true
},
ConfigService
...
]
Run Code Online (Sandbox Code Playgroud)
config-service.ts
@Injectable()
export class ConfigService
load(router: Router): Promise<any> {
console.log('current url : ' + router.url);
return new Promise(((resolve, reject) => resolve()));
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是我越来越
Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppBrowserModule in ./AppBrowserModule@-1:-1
Run Code Online (Sandbox Code Playgroud)
我也尝试Injector在构造函数中使用,但是它也不起作用。
我正在尝试做的事可行吗?
config-service.ts可以如下重写。
@Injectable()
export class ConfigService
constructor(private injector: Injector){}
load(): Promise<any> {
const router = this.injector.get(Router);
console.log('current url : ' + router.url);
return new Promise(((resolve, reject) => resolve()));
}
}
Run Code Online (Sandbox Code Playgroud)
无需在app.module.ts中将Router注入为依赖项。
我试图做的事情是不可行的,因为 APP_INITIALIZER 发生在路由器初始化之前。 https://medium.com/hackernoon/hook-into-angular-initialization-process-add41a6b7e
| 归档时间: |
|
| 查看次数: |
5025 次 |
| 最近记录: |