@angular/core#NgModuleRef TypeScript Examples

The following examples show how to use @angular/core#NgModuleRef. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: route.component.ts    From router with MIT License 6 votes vote down vote up
private loadAndRender(route: Route) {
    if (route.load) {
      return from(
        route
          .load()
          .then(
            (componentOrModule: NgModuleRef<ModuleWithRoute> | Type<any>) => {
              let component: Type<any>;

              if ((componentOrModule as any).ɵmod) {
                const moduleRef: NgModuleRef<ModuleWithRoute> =
                  createNgModuleRef(
                    componentOrModule as Type<any>,
                    this.viewContainerRef.injector
                  );
                component = moduleRef.instance.routeComponent;
              } else {
                component = componentOrModule as Type<any>;
              }

              this.renderComponent(component);
            }
          )
      );
    } else {
      this.showTemplate();
      return of(true);
    }
  }
Example #2
Source File: hmr.ts    From angular-material-admin with MIT License 6 votes vote down vote up
hmrBootstrap = (module: any, bootstrap: () => Promise<NgModuleRef<any>>) => {
  let ngModule: NgModuleRef<any>;
  module.hot.accept();
  bootstrap().then(mod => ngModule = mod);
  module.hot.dispose(() => {
    const appRef: ApplicationRef = ngModule.injector.get(ApplicationRef);
    const elements = appRef.components.map(c => c.location.nativeElement);
    const makeVisible = createNewHosts(elements);
    ngModule.destroy();
    makeVisible();
  });
}
Example #3
Source File: create-app-ng-module-ref.pipe.ts    From angular-dream-stack with MIT License 6 votes vote down vote up
transform(
    appRegistration: AppRegistration,
    parentInjector: Injector
  ): Observable<NgModuleRef<LoadableApp>> {
    return this.appLoader
      .createNgModuleRef(appRegistration, parentInjector)
      .pipe(
        catchError((error) => {
          console.error('Error when creating app NgModule: ', error);
          return throwError(error);
        })
      );
  }
Example #4
Source File: hmr.ts    From youpez-admin with MIT License 6 votes vote down vote up
hmrBootstrap = (module: any, bootstrap: () => Promise<NgModuleRef<any>>) => {
  let ngModule: NgModuleRef<any>
  module.hot.accept()
  bootstrap().then(mod => ngModule = mod)
  module.hot.dispose(() => {
    const appRef: ApplicationRef = ngModule.injector.get(ApplicationRef)
    const elements = appRef.components.map(c => c.location.nativeElement)
    const makeVisible = createNewHosts(elements)
    ngModule.destroy()
    makeVisible()
  })
}
Example #5
Source File: platform-core.ts    From angular-miniprogram with MIT License 6 votes vote down vote up
protected linkNgComponentWithPage(
    mpComponentInstance: MiniProgramComponentInstance,
    componentRef: ComponentRef<unknown>,
    ngModuleRef: NgModuleRef<unknown>
  ) {
    mpComponentInstance.__isLink = true;
    mpComponentInstance.__ngComponentHostView = componentRef.hostView;
    mpComponentInstance.__ngComponentInstance = componentRef.instance;
    mpComponentInstance.__ngComponentInjector = componentRef.injector;
    const ngZone = componentRef.injector.get(NgZone);
    mpComponentInstance.__ngZone = ngZone;
    const { lView, id }: { lView: LView; id: number } =
      findPageLView(componentRef);
    setLViewPath(lView, [id]);
    mpComponentInstance.__completePath = [id];
    ngZone.runOutsideAngular(() => {
      const initValue = getPageRefreshContext(lView);
      mpComponentInstance.setData(initValue);
    });
    lViewLinkToMPComponentRef(mpComponentInstance, lView);
    mpComponentInstance.__lView = lView;
    mpComponentInstance.__ngDestroy = () => {
      ngModuleRef.destroy();
      componentRef.destroy();
      removePageLViewLink(id);
      cleanAll(lView);
    };
  }
Example #6
Source File: render-app.directive.ts    From angular-dream-stack with MIT License 5 votes vote down vote up
currentNgModuleRef?: NgModuleRef<unknown>;
Example #7
Source File: app-container.component.ts    From angular-dream-stack with MIT License 5 votes vote down vote up
@Input() ngModuleRef: NgModuleRef<LoadableApp>;
Example #8
Source File: platform-core.ts    From angular-miniprogram with MIT License 4 votes vote down vote up
public pageStartup = (
    module: Type<unknown>,
    component: Type<unknown>,
    pageOptions?: { useComponent: boolean }
  ) => {
    const _this = this;
    if (pageOptions?.useComponent) {
      const options = this.getComponentOptions<true>(component) || {};
      const config: WechatMiniprogram.Component.Options<{}, {}, {}, {}, true> =
        {
          ...options,
          data: { hasLoad: false },
          options: { ...options?.options, multipleSlots: true },
          methods: {
            ...this.listenerEvent(component),
            onHide: async function (this: MiniProgramComponentInstance) {
              if (options.methods?.onHide) {
                await options.methods.onHide.bind(this)();
              }
              _this.pageStatus.detachView.bind(this)();
            },
            onUnload: async function (this: MiniProgramComponentInstance) {
              if (options.methods?.onUnload) {
                await options.methods.onUnload.bind(this)();
              }
              _this.pageStatus.destroy.bind(this)();
            },
            onShow: async function (this: MiniProgramComponentInstance) {
              if (options.methods?.onShow) {
                await options.methods.onShow.bind(this)();
              }
              _this.pageStatus.attachView.bind(this)();
            },
          },
        };
      config.lifetimes = config.lifetimes || {};
      const oldCreated = config.lifetimes.created;
      let componentRef: ComponentRef<unknown>,
        ngModuleRef: NgModuleRef<unknown>;
      config.lifetimes.created = function (this: MiniProgramComponentInstance) {
        const app = getApp<AppOptions>();
        const result = app.__ngStartPage(module, component, this);
        componentRef = result.componentRef;
        ngModuleRef = result.ngModuleRef;
        if (oldCreated) {
          oldCreated.bind(this)();
        }
      };
      const oldAttached = config.lifetimes.attached;
      config.lifetimes.attached = function (
        this: MiniProgramComponentInstance
      ) {
        _this.linkNgComponentWithPage(this, componentRef, ngModuleRef);
        if (oldAttached) {
          oldAttached.bind(this)();
        }
      };
      return Component(config);
    }
    const options = this.getPageOptions(component) || {};
    return Page({
      ...options,
      ...this.listenerEvent(component),
      data: { hasLoad: false },
      onLoad: function (this: MiniProgramComponentInstance, query) {
        const app = getApp<AppOptions>();
        const { componentRef, ngModuleRef } = app.__ngStartPage(
          module,
          component,
          this
        );
        _this.linkNgComponentWithPage(this, componentRef, ngModuleRef);
        if (options.onLoad) {
          return options.onLoad.bind(this)(query);
        }
      },
      onHide: async function (this: MiniProgramComponentInstance) {
        if (options.onHide) {
          await options.onHide.bind(this)();
        }
        _this.pageStatus.detachView.bind(this)();
      },
      onUnload: async function (this: MiniProgramComponentInstance) {
        if (options.onUnload) {
          await options.onUnload.bind(this)();
        }
        _this.pageStatus.destroy.bind(this)();
      },
      onShow: async function (this: MiniProgramComponentInstance) {
        if (options.onShow) {
          await options.onShow.bind(this)();
        }
        _this.pageStatus.attachView.bind(this)();
      },
    });
  };