简体   繁体   English

使用ngComponentOutlet创建动态组件的依赖注入问题

[英]Dependency Injection problem for creating dynamic components with ngComponentOutlet

The project requires to create components dynamically by using ngComponentOutlet directive. 该项目需要使用ngComponentOutlet指令动态创建组件。 The dynamic components will receive data by injecting it into the constructor. 动态组件将通过将数据注入到构造函数中来接收数据。 So, how can I pass this data as a parameter in the constructor? 那么,如何在构造函数中将此数据作为参数传递呢?

I have created a sample and the link is https://angular-lqaeqp.stackblitz.io/load 我创建了一个示例,链接为https://angular-lqaeqp.stackblitz.io/load

The project structure is: 项目结构为:

  1. HomeComponent - The starting point HomeComponent-起点

  2. LoadComponents Module - A lazy loaded module which has 2 components LoadComponents模块-延迟加载的模块,其中包含2个组件

    (i) LoadComponents - The default for route '/load' (i)LoadComponents-路线'/ load'的默认设置

    (ii) Component1Component - The dynamic component that will be created from LoadComponents (ii)Component1Component-将由LoadComponents创建的动态组件

The LoadComponents has the following code for the creation: LoadComponents具有以下用于创建的代码:

<ng-container *ngComponentOutlet="component;injector: injectData;"></ng-container>

  1. Content Model - A model that needs to be injected in Component1Component 内容模型-需要在Component1Component中注入的模型

If I remove the injection code then the app works, otherwise it shows the error: 如果我删除了注入代码,则该应用程序将运行,否则将显示错误:

Error: StaticInjectorError(AppModule)[Component1Component -> Content]

For the time being I have solved the project issue by using the plugin "ng-dynamic-component", which works like a charm. 目前,我已经通过使用插件“ ng-dynamic-component”解决了项目问题,该插件的工作原理很吸引人。 But I have to apply Angular's ngComponentOutlet directive. 但是我必须应用Angular的ngComponentOutlet指令。

You are injecting Content to your component, therefore it should be an injectable: 您正在向组件中注入Content ,因此它应该是可注入的:

@Injectable({
  providedIn: 'root',
})
export class Content {
    @Input() public Code: string;
    @Input() public HTML: string;
}

Your fixed StackBlitz 您固定的StackBlitz

PS. PS。 Always include the troubling code in your question! 始终在您的问题中包含令人烦恼的代码!

Service and Component are different purpose in Angular 服务和组件在Angular中的用途不同

Service is a broad category encompassing any value, function, or feature that an app needs. 服务是一个广泛的类别,涵盖了应用程序需要的任何价值,功能或功能。 A service is typically a class with a narrow, well-defined purpose. 服务通常是具有狭窄,明确定义目的的类。 It should do something specific and do it well. 它应该做特定的事情并且做得很好。

Angular distinguishes components from services to increase modularity and reusability. Angular将组件与服务区分开来,以提高模块化和可重用性。 By separating a component's view-related functionality from other kinds of processing, you can make your component classes lean and efficient. 通过将组件的与视图相关的功能与其他类型的处理分开,可以使组件类更加精简和高效。

Since you are Using Injectable decorator inside content.module.ts you should not use @Input decorator. 由于您在content.module.ts中使用Injectable装饰器,因此不应使用@Input装饰器。 Then don't initialize Object with new Key word. 然后不要用新的关键字初始化对象。 Instantiating an object with new keyword is used to create objects that are not injectable. 使用new关键字实例化对象用于创建不可注入的对象。 Ref: Angular2 - calling constructor() vs new keyword to create an object? 参考: Angular2-调用Constructor()与new关键字创建对象?

content.model.ts content.model.ts

import { Injectable } from '@angular/core';    
@Injectable({
  providedIn: 'root',
})
export class Content {
   Code: string ;
   HTML: string;
}

Example: https://stackblitz.com/edit/angular-favvmz 示例: https//stackblitz.com/edit/angular-favvmz

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

相关问题 如何对动态 Angular 组件进行依赖注入 - How to do dependency injection for Dynamic Angular Components Angular 4 ngComponentOutlet加载动态组件,但不呈现路径内容 - Angular 4 ngComponentOutlet loads dynamic components, but route content is not being rendered Angular 7:注入 ngComponentOutlet 的依赖项 - Angular 7: injection with dependencies for ngComponentOutlet AngularJS 2依赖注入到子组件中 - AngularJS 2 dependency injection into sub-child components 依赖注入创建多个实例 - dependency injection creating multiple instances Angular2 - 在组件外部使用依赖注入 - Angular2 - Use Dependency Injection outside Components 如何使用ngComponentOutlet对多种类型的组件进行ng? - How to ngFor on multiple types of components using ngComponentOutlet? 如何使用ng-dynamic-component的DynamicModule.withComponents而不为这些组件创建依赖关系? - How to use DynamicModule.withComponents of ng-dynamic-component without creating dependency for those components? Angular 4 如何使用 NgComponentOutlet 将数据传递到动态组件 - Angular 4 how to pass data into dynamic component with NgComponentOutlet Angular 2(Typescript) - 远程开发组件和动态注入 - Angular 2 (Typescript) - Remote development of components and dynamic injection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM