如何以角度在 CustomReuseStrategy 中注入服务? [英] How to inject a service in CustomReuseStrategy in angular?

查看:24
本文介绍了如何以角度在 CustomReuseStrategy 中注入服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Angular 中实现了 CustomReuseStrategy.我想订阅 shouldAttach() 中的一个 observable 以确定是否应该重新附加路由.我在服务中创建了一个 observable,我需要在 CustomReuseStrategy 中使用服务实例.我尝试在构造函数中注入服务,但出现此错误:

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:

无法解析 CustomReuseStrategy 的所有参数

Can't resolve all parameters for CustomReuseStrategy

RouteReuseStrategy 的构造函数应该没有参数吗?如果是这样,我该如何创建服务的实例?

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

推荐答案

我已经使用反射注入器解决了这个问题.

I have resolved this by using Reflective Injector.

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

这篇关于如何以角度在 CustomReuseStrategy 中注入服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆