How to inject a service in CustomReuseStrategy in angular?

放肆的年华 提交于 2019-12-11 05:46:58

问题


I have implemented CustomReuseStrategy in Angular. I want to subscribe to an observable in shouldAttach() to determine if the route should be reattached or not. I have created an observable in a service and I need to use the service instance in CustomReuseStrategy. I tried injecting the service in the constructor and I get this error:

Can't resolve all parameters for CustomReuseStrategy

Should the constructor for the RouteReuseStrategy not have parameters? If so, how do I create the instance of the service?


回答1:


I have resolved this by using Reflective Injector.

let injector = ReflectiveInjector.resolveAndCreate([MyService]);
this.subscriptionSvc = injector.get(MyService);


来源:https://stackoverflow.com/questions/48197965/how-to-inject-a-service-in-customreusestrategy-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!