【问题标题】:How to inject a service in CustomReuseStrategy in angular?如何以角度在 CustomReuseStrategy 中注入服务?
【发布时间】:2018-04-17 05:42:08
【问题描述】:

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

无法解析 CustomReuseStrategy 的所有参数

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

【问题讨论】:

  • 你的应用模块是什么样的?您是否在 providers 数组中声明您的服务?
  • 是的。我已经在 appmodule 的 Providers 数组中声明了我的服务。我可以将此服务注入其他组件,它们运行良好。因为“CustomReuseStrategy”是一个类而不是一个组件。我无法通过构造函数注入服务。
  • 请提供所有相关代码。没有它,这个问题就离题了。见stackoverflow.com/help/mcve

标签: angular angular-ui-router angular-ui-router-extras


【解决方案1】:

我已经通过使用反射式注射器解决了这个问题。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2020-01-03
    • 2018-06-19
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多