Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: moduleFactory.create is not a function. #3

Open
ghost opened this issue Jul 11, 2021 · 1 comment
Open

TypeError: moduleFactory.create is not a function. #3

ghost opened this issue Jul 11, 2021 · 1 comment

Comments

@ghost
Copy link

ghost commented Jul 11, 2021

Hi,
Thank you for your great work here!
I have tried to get the plugin architecture to work.
I created the plugin and can build it successfully. The plugin Loader itself just works fine while loading the javascript source file. The result of the pluginLoader load function (the moduleFactory object) looks like this
image

The load Plugin Function itself gives me the error massage TypeError: moduleFactory.create is not a function. (In 'moduleFactory.create(this.injector)', 'moduleFactory.create' is undefined) in the resolve part of the load function! Can you point me with that information what I am doin wrong?

Thanks For Help

@faileon
Copy link

faileon commented Oct 27, 2022

Late to the party but I believe the factories are deprecated in newer Angular versions. Now to instantiate the module you can do it like this:

import { createNgModule } from '@angular/core';

const moduleRef = createNgModule(moduleClass);

Later when you want to instantiate a component you should use the component class directly. Question is how to obtain the component classes from instantiated module. You can perhaps use the bootstrap components and/or entryComponents of the module, but I did not find a non-hacky way to access those properties. Instead I solved it with custom DI tokens.

// get hold of some component via DI -> 'entry' is a custom string token in the loaded module
const entryComponentClass = moduleRef.injector.get('entry');

this.viewContainerRef.createComponent(entryComponentClass, {
      injector: moduleRef.injector,
      ngModuleRef: moduleRef
    });

In the plugin's module:

@NgModule({
  imports: [CommonModule],
  declarations: [PluginEntryComponent],
  providers: [
    {
      provide: 'entry',
      useValue: PluginEntryComponent,
    },
  ],
})
export class SomePluginModule{}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant