How to inject a service in CustomReuseStrategy in

2019-08-17 04:44发布

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条回答
2楼-- · 2019-08-17 05:07

I have resolved this by using Reflective Injector.

let injector = ReflectiveInjector.resolveAndCreate([MyService]);
this.subscriptionSvc = injector.get(MyService);
查看更多
登录 后发表回答