根中的Angular2动态组件注入(inject)

标签 angular angular2-components

问题

我正在寻找将已知/定义的组件注入(inject)应用程序的根并将 @Input() 选项投影到该组件的最佳方法。

要求

这对于在应用程序主体中创建模态/工具提示之类的东西是必要的,这样 overflow:hidden/etc 就不会扭曲位置或将其完全切断。

研究

我发现我可以获得 ApplicationRef,然后巧妙地向上遍历并找到 ViewContainerRef

constructor(private applicationRef: ApplicationRef) {
}

getRootViewContainerRef(): ViewContainerRef {
  return this.applicationRef['_rootComponents'][0]['_hostElement'].vcRef;
}

一旦我有了它,我就可以像这样在 ref 上调用 createComponent:

appendNextToLocation<T>(componentClass: Type<T>, location: ViewContainerRef): ComponentRef<T> {
  const componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentClass);
  const parentInjector = location.parentInjector;
  return location.createComponent(componentFactory, location.length, parentInjector);
}

但是现在我已经创建了组件,但是我的 Input 属性都没有实现。为了实现这一点,我必须手动遍历我的选项并将它们设置为 appendNextToLocation 实例的结果,例如:

const props = Object.getOwnPropertyNames(options);
for(const prop of props) {
  component.instance[prop] = options[prop];
}

现在我确实意识到您可以做一些 DI 来注入(inject)选项,但这使得它在尝试用作普通组件时无法重复使用。这是供引用的样子:

let componentFactory = this.componentFactoryResolver.resolveComponentFactory(ComponentClass);
let parentInjector = location.parentInjector;

let providers = ReflectiveInjector.resolve([
  { provide: ComponentOptionsClass, useValue: options }
]);

childInjector = ReflectiveInjector.fromResolvedProviders(providers, parentInjector);

return location.createComponent(componentFactory, location.length, childInjector);

综上所述,以上所有实际上都有效,但有时感觉有点老套。我还担心像上面那样设置输入属性的生命周期时间,因为它发生在创建之后。

著名引用文献

最佳答案

在 2.3.0 中,引入了 attachView,它允许您能够将更改检测附加到 ApplicationRef,但是,您仍然需要手动将元素附加到根容器。这是因为对于 Angular2,其运行环境的可能性可能是 web workers、universal、nativescript 等,因此我们需要明确地告诉它我们希望将其添加到 View 的位置/方式。

下面是一个示例服务,它允许您动态插入组件并自动投影组件的 Input

import {
  ApplicationRef, ComponentFactoryResolver, ComponentRef, Injectable,
  Injector, ViewContainerRef, EmbeddedViewRef, Type
} from '@angular/core';

/**
 * Injection service is a helper to append components
 * dynamically to a known location in the DOM, most
 * noteably for dialogs/tooltips appending to body.
 * 
 * @export
 * @class InjectionService
 */
@Injectable()
export class InjectionService {
  private _container: ComponentRef<any>;

  constructor(
    private applicationRef: ApplicationRef,
    private componentFactoryResolver: ComponentFactoryResolver,
    private injector: Injector) {
  }

  /**
   * Gets the root view container to inject the component to.
   * 
   * @returns {ComponentRef<any>}
   * 
   * @memberOf InjectionService
   */
  getRootViewContainer(): ComponentRef<any> {
    if(this._container) return this._container;

    const rootComponents = this.applicationRef['_rootComponents'];
    if (rootComponents.length) return rootComponents[0];

    throw new Error('View Container not found! ngUpgrade needs to manually set this via setRootViewContainer.');
  }

  /**
   * Overrides the default root view container. This is useful for 
   * things like ngUpgrade that doesn't have a ApplicationRef root.
   * 
   * @param {any} container
   * 
   * @memberOf InjectionService
   */
  setRootViewContainer(container): void {
    this._container = container;
  }

  /**
   * Gets the html element for a component ref.
   * 
   * @param {ComponentRef<any>} componentRef
   * @returns {HTMLElement}
   * 
   * @memberOf InjectionService
   */
  getComponentRootNode(componentRef: ComponentRef<any>): HTMLElement {
    return (componentRef.hostView as EmbeddedViewRef<any>).rootNodes[0] as HTMLElement;
  }

  /**
   * Gets the root component container html element.
   * 
   * @returns {HTMLElement}
   * 
   * @memberOf InjectionService
   */
  getRootViewContainerNode(): HTMLElement {
    return this.getComponentRootNode(this.getRootViewContainer());
  }

  /**
   * Projects the inputs onto the component
   * 
   * @param {ComponentRef<any>} component
   * @param {*} options
   * @returns {ComponentRef<any>}
   * 
   * @memberOf InjectionService
   */
  projectComponentInputs(component: ComponentRef<any>, options: any): ComponentRef<any> {
    if(options) {
      const props = Object.getOwnPropertyNames(options);
      for(const prop of props) {
        component.instance[prop] = options[prop];
      }
    }

    return component;
  }

  /**
   * Appends a component to a adjacent location
   * 
   * @template T
   * @param {Type<T>} componentClass
   * @param {*} [options={}]
   * @param {Element} [location=this.getRootViewContainerNode()]
   * @returns {ComponentRef<any>}
   * 
   * @memberOf InjectionService
   */
  appendComponent<T>(
    componentClass: Type<T>, 
    options: any = {}, 
    location: Element = this.getRootViewContainerNode()): ComponentRef<any> {

    let componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentClass);
    let componentRef = componentFactory.create(this.injector);
    let appRef: any = this.applicationRef;
    let componentRootNode = this.getComponentRootNode(componentRef);

    // project the options passed to the component instance
    this.projectComponentInputs(componentRef, options);

    appRef.attachView(componentRef.hostView);

    componentRef.onDestroy(() => {
      appRef.detachView(componentRef.hostView);
    });

    location.appendChild(componentRootNode);

    return componentRef;
  }
}

关于根中的Angular2动态组件注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39857222/

相关文章:

javascript - 没有为 node_modules 中的库定义 Angular2 require

javascript - Angular 2如何将对象数组放入另一个对象数组

javascript - 如何在父组件中调用子组件的函数

Angular 2 : dynamic selector property

angular - 将自定义表单控件连接到 Angular 2 中的父表单验证

Angular:在进行内容投影时获取对父组件的引用

angular - 仅当可观察流为空时才显示 div

Angular mat-select,将选定的值发送到函数

angular - 兄弟组件之间如何通信

Angular2 - 服务未在构造函数和 ngOnInit 之外定义?