ViewChild не находит элемент при использовании AoT - PullRequest
0 голосов
/ 28 марта 2019

Я нашел замечательного декоратора, который хотел использовать в моем проекте.Но при использовании AoT ViewChild работает неправильно.Старая проблема, десятки открытых заявок и тому подобное.

import { ElementRef, ViewChild } from '@angular/core';

/**
 * To get the native element instead of ElementRef.nativeElement
 * Caveats: it won’t work with AOT
 * @param selector Selector. Example: <div #selector></div>
 */
export function NativeElement(selector: string): PropertyDecorator {
  // Create the viewchild decorator with read: ElementRef
  const propDecoratorFn: Function = ViewChild(selector, { read: ElementRef });

  // Target is the component object
  // Property key is the name of the property
  return function(target: Object, propertyKey: string | symbol): void {
    // Somehow this will execute the viewchild process to attach a new property called tempViewChildFor_
    // to the target (component object instance)
    propDecoratorFn(target, `tempViewChildFor_${propertyKey}`);

    // Redefined the property with getter. It will return the tempViewChildFor_ with nativeElement
    Object.defineProperty(target, propertyKey, {
      get: function(): HTMLElement {
        const property = this[`tempViewChildFor_${propertyKey}`];
        if (!property) {
          throw Error(
            'Can not get ElementRef using selector, please check your component view again.'
          );
        }

        return property.nativeElement;
      }
    });
  };
}

Источник кода

Есть ли обходной путь, когда AoT включен?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...