我正在尝试检索 APP_INITIALIZER 中 url 中存在的数据
应用程序模块.ts
export function init(config: ConfigService, router: Router) {
return () => config.load(router);
}
providers : [
...
{
provide: APP_INITIALIZER,
useFactory: init,
deps: [ConfigService, Router],
multi: true
},
ConfigService
...
]
配置服务.ts
@Injectable()
export class ConfigService
load(router: Router): Promise<any> {
console.log('current url : ' + router.url);
return new Promise(((resolve, reject) => resolve()));
}
}
不幸的是我得到了
Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppBrowserModule in ./AppBrowserModule@-1:-1
我也尝试使用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()));
}
}
无需将 Router 作为依赖项注入 app.module.ts 中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)