从导出功能创建组件

时间:2017-07-14 08:15:41

标签: angular function import export components

基于this question我知道可以提取组件导出组件的信息。下一步应该是创建一个对象

//our root app component
import {Component, NgModule, Type, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'


@NgModule({
})
export class FirstModule {}


@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
  `,
})
export class App {
  name:string;
  constructor() {
    this.name = `Angular! v${VERSION.full}`
  }
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  exports: [FirstModule],
  bootstrap: [ App ]
})
export class AppModule {}

declare let Reflect: any;
function getAnnotations(typeOrFunc: Type<any>): any[]|null {
  // Prefer the direct API.
  if ((<any>typeOrFunc).annotations) {
    let annotations = (<any>typeOrFunc).annotations;
    if (typeof annotations === 'function' && annotations.annotations) {
      annotations = annotations.annotations;
    }
    return annotations;
  }

  // API of tsickle for lowering decorators to properties on the class.
  if ((<any>typeOrFunc).decorators) {
    return convertTsickleDecoratorIntoMetadata((<any>typeOrFunc).decorators);
  }

  // API for metadata created by invoking the decorators.
  if (Reflect && Reflect.getOwnMetadata) {
    return Reflect.getOwnMetadata('annotations', typeOrFunc);
  }
  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);
  });
}


const browserAnnotations = getAnnotations(BrowserModule);
const annotations = getAnnotations(AppModule);

console.log(browserAnnotations[0].exports);
console.log(annotations[0].exports);

结果是一个与组件同名的功能 但是如何创建一个工作组件来使用它,例如在应用程序的路由?

0 个答案:

没有答案