错误错误:未捕获(在承诺中):错误:StaticInjectorError(AppModule)[ElementRef]:StaticInjectorError(Platform:core)[ElementRef]:NullInjectorError:没有ElementRef的提供者!错误:StaticInjectorError(AppModule)[ElementRef]:StaticInjectorError(Platform:core)[ElementRef]:NullInjectorError:没有ElementRef的提供者!
当我在我的一个服务中使用 ElementRef
时,这是我在angular5中得到的错误,我确实像这样导入它 .
import { Injectable,ElementRef } from '@angular/core';
2 回答
你不能将ElementRef注入服务类,
它仅用于组件或指令,
维护服务类只写业务逻辑,
并尝试在组件中使用ElementRef
或创建一个指令并尝试使用该指令 .
有关详细信息,请参阅以下答案链接,
Injecting ElementRef to injecable error
添加:
要
.angular-cli.json
文件中的默认值,希望有所帮助 .