首页 文章

在角度服务中使用“ElementRef”时出错

提问于
浏览
0

错误错误:未捕获(在承诺中):错误: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 回答

  • 2

    你不能将ElementRef注入服务类,

    它仅用于组件或指令,

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

    并尝试在组件中使用ElementRef

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

    有关详细信息,请参阅以下答案链接,

    Injecting ElementRef to injecable error

  • 0

    添加:

    "build": {
      "preserveSymlinks": true
    },
    

    .angular-cli.json 文件中的默认值,希望有所帮助 .

相关问题