简体   繁体   English

错误:使用 Renderer2 创建 Angular 服务时出现 StaticInjectorError(AppModule)[SomeComponent -> Renderer2]

[英]Error: StaticInjectorError(AppModule)[SomeComponent -> Renderer2] when creating an Angular service with Renderer2

I am trying to create a service to inject a <script></script > in my DOM, instead of doing this via my Component.我正在尝试创建一个服务以在我的 DOM 中注入一个<script></script >,而不是通过我的组件来执行此操作。

My implementation of Renderer2 works perfectly when using it in a Component, but when using Rendere2 via a service I get this error:在组件中使用 Renderer2 时,我的实现完美运行,但是通过服务使用 Rendere2 时出现此错误:

core.js:15724 ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[ContactUsPageComponent -> Renderer2]: core.js:15724 错误错误:未捕获(承诺中):错误:StaticInjectorError(AppModule)[ContactUsPageComponent -> Renderer2]:
StaticInjectorError(Platform: core)[ContactUsPageComponent -> Renderer2]: NullInjectorError: No provider for Renderer2: 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]: StaticInjectorError(平台:核心)[ContactUsPageComponent -> Renderer2]:

I have tried importing Renderer2 in my AppModule, but get this notification:我尝试在我的 AppModule 中导入 Renderer2,但收到此通知:

(alias) class Renderer2 import Renderer2 Extend this base class to implement custom rendering. (别名) class Renderer2 import Renderer2 扩展此基础 class 以实现自定义渲染。 By default, Angular renders a template into DOM.默认情况下,Angular 将模板渲染到 DOM 中。 You can use custom rendering to intercept rendering calls, or to render to something other than DOM.你可以使用自定义渲染来拦截渲染调用,或者渲染到 DOM 以外的东西。

Create your custom renderer using RendererFactory2.使用 RendererFactory2 创建您的自定义渲染器。

Use a custom renderer to bypass Angular's templating and make custom UI changes that can't be expressed declaratively.使用自定义渲染器绕过 Angular 的模板并进行无法以声明方式表达的自定义 UI 更改。 For example if you need to set a property or an attribute whose name is not statically known, use the setProperty() or setAttribute() method.例如,如果您需要设置名称静态未知的属性或属性,请使用 setProperty() 或 setAttribute() 方法。

This is my service implementation:这是我的服务实现:

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);
  }
}

And I simply call it from a component using: this.chatbotService.showChatbot();我只是从一个组件中调用它: this.chatbotService.showChatbot();

Any suggestions o how to implement this correctly would be greatly appreciated!任何关于如何正确实施的建议将不胜感激!

Here is the answer to my question:这是我的问题的答案:

NOTE: to use Renderer2 in a service, you need to create an instance of Renderer2, using RendererFactory2.注意:要在服务中使用 Renderer2,您需要使用 RendererFactory2 创建 Renderer2 的实例。 From there it is straight forward.从那里它是直截了当的。

Here is my Service code:这是我的服务代码:

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);
  }
}

Here is my Component call: this.chatbotService.showChatbot();这是我的组件调用: this.chatbotService.showChatbot();

Hope this helps someone else!希望这对其他人有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM