javascript - 错误 : StaticInjectorError(AppModule)[SomeComponent -> Renderer2] when creating an Angular service with Renderer2

标签 javascript angular

我正在尝试创建一个服务来注入(inject) <script></script > 在我的 DOM 中,而不是通过我的组件执行此操作。

我的 Renderer2 实现在组件中使用时完美运行,但通过服务使用 Rendere2 时出现此错误:

core.js:15724 ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[ContactUsPageComponent -> Renderer2]:
StaticInjectorError(Platform: core)[ContactUsPageComponent -> Renderer2]: NullInjectorError: No provider for Renderer2! Error: StaticInjectorError(AppModule)[ContactUsPageComponent -> Renderer2]:
StaticInjectorError(Platform: core)[ContactUsPageComponent -> Renderer2]:

我已尝试在我的 AppModule 中导入 Renderer2,但收到此通知:

(alias) class Renderer2 import Renderer2 Extend this base class to implement custom rendering. By default, Angular renders a template into DOM. You can use custom rendering to intercept rendering calls, or to render to something other than DOM.

Create your custom renderer using RendererFactory2.

Use a custom renderer to bypass Angular's templating and make custom UI changes that can't be expressed declaratively. For example if you need to set a property or an attribute whose name is not statically known, use the setProperty() or setAttribute() method.

这是我的服务实现:

import { Injectable, Renderer2, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';

@Injectable({
  providedIn: 'root'
})
export class ChatbotService {

  constructor(private renderer2: Renderer2, @Inject(DOCUMENT) private document) {}

  showChatbot() {
    const s = this.renderer2.createElement('script');
    s.type = 'text/javascript';
    s.id = 'ze-snippet';
    s.src = 'https://static.zdassets.com/ekr/snippet.js?key=7e...';
    s.text = ``;
    this.renderer2.appendChild(this.document.body, s);
  }
}

我只是从一个组件调用它,使用:this.chatbotService.showChatbot();

如有任何有关如何正确实现的建议,我们将不胜感激!

最佳答案

这是我的问题的答案:

注意:要在服务中使用 Renderer2,您需要使用 RendererFactory2 创建 Renderer2 的实例。从那里开始是直截了当的。

这是我的服务代码:

import { Injectable, Renderer2, RendererFactory2, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';

@Injectable({
  providedIn: 'root'
})
export class ChatbotService {
  private renderer: Renderer2;

  constructor(private rendererFactory: RendererFactory2, @Inject(DOCUMENT) private document) {
    this.renderer = rendererFactory.createRenderer(null, null);
  }

  showChatbot() {
    const s = this.renderer.createElement('script');
    s.type = 'text/javascript';
    s.id = 'ze-snippet';
    s.src = 'https://static.zdassets.com/ekr/snippet.js?key=7e...';
    s.text = ``;
    this.renderer.appendChild(this.document.body, s);
  }
}

这是我的组件调用: this.chatbotService.showChatbot();

希望这对其他人有帮助!

关于javascript - 错误 : StaticInjectorError(AppModule)[SomeComponent -> Renderer2] when creating an Angular service with Renderer2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58989990/

相关文章:

Angular 4嵌套路由器导出

javascript - 我应该使用哪种 D3 布局来显示关系?

加载外部csv文件的javascript库

javascript - 硬关闭窗口时未触发 Socket.IO 套接字断开连接事件

javascript - 'class'实例创建时的回调

javascript - 重试对 Angular 中另一个来源的失败 API 调用

angular - RxJS 中的管道有什么用?

javascript - 未捕获的语法错误 : Unexpected identifier with import statement

Angular 5 和 Service Worker : How to exclude a particular path from ngsw-config. json

javascript - 用于仪表板应用程序的 Angular 7 多个路由器 socket