Is there a way to get the HTML template for an Angular* component?

后端 未结 2 566
-上瘾入骨i
-上瘾入骨i 2020-12-17 00:42

I\'m trying to create a library of shared Angular components to use across a number of different web projects. Along with the shared component library, I\'m trying to create

2条回答
  •  既然无缘
    2020-12-17 01:23

    You can try using special function to get annotations:

    annotation.ts

    declare let Reflect: any;
    export function getAnnotation(typeOrFunc: Type): any[]|null {
      // Prefer the direct API.
      if ((typeOrFunc).annotations) {
        let annotations = (typeOrFunc).annotations;
        if (typeof annotations === 'function' && annotations.annotations) {
          annotations = annotations.annotations;
        }
        return annotations[0];
      }
    
      // API of tsickle for lowering decorators to properties on the class.
      if ((typeOrFunc).decorators) {
        return convertTsickleDecoratorIntoMetadata((typeOrFunc).decorators)[0];
      }
    
      // API for metadata created by invoking the decorators.
      if (Reflect && Reflect.getOwnMetadata) {
        return Reflect.getOwnMetadata('annotations', typeOrFunc)[0];
      }
      return null;
    }
    
    function convertTsickleDecoratorIntoMetadata(decoratorInvocations: any[]): any[] {
      if (!decoratorInvocations) {
        return [];
      }
      return decoratorInvocations.map(decoratorInvocation => {
        const decoratorType = decoratorInvocation.type;
        const annotationCls = decoratorType.annotationCls;
        const annotationArgs = decoratorInvocation.args ? decoratorInvocation.args : [];
        return new annotationCls(...annotationArgs);
      });
    }
    

    and then

    this.encodedExampleHtml = getAnnotation(FooComponent).template;
    

    Plunker Example

    See also

    • When and how s decorator applied to the decorated classes from the @angular packages
    • Read and List all exports of an angular module

    • Example of how angular parses template

提交回复
热议问题