【问题标题】:Error while using "ElementRef" in one of the service in angular在 Angular 的一项服务中使用“ElementRef”时出错
【发布时间】:2018-12-12 12:58:45
【问题描述】:

ERROR 错误:未捕获(承诺):错误: StaticInjectorError(AppModule)[ElementRef]:
StaticInjectorError(平台:核心)[ElementRef]: NullInjectorError:没有提供 ElementRef!错误:StaticInjectorError(AppModule)[ElementRef]:
StaticInjectorError(平台:核心)[ElementRef]: NullInjectorError: ElementRef 没有提供者!

这是我在我的一项服务中使用 ElementRef 时在 angular5 中遇到的错误,我确实像这样导入它:

import { Injectable,ElementRef } from '@angular/core';

【问题讨论】:

  • 请提供一些代码。

标签: angular elementref


【解决方案1】:

您不能将 ElementRef 注入服务类,

它只用于组件或指令,

维护服务类只写业务逻辑,

并尝试在您的组件中使用 ElementRef

或创建一个指令并尝试使用该指令。

更多详情可以参考下面的回答链接,

Injecting ElementRef to injecable error

【讨论】:

  • 无论如何你都应该使用组件 :) 将组件视为可以跟踪不同状态的父组件(因此将知道何时以及如何根据您告诉它的方式与服务交互) .它允许您将业务逻辑留给服务,因此它们也不会跟踪组件已经是什么。
猜你喜欢
  • 1970-01-01
  • 2017-05-10
  • 1970-01-01
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多