dkhrunov / ngx-mfe

Angular library for working with micro-frontends in Webpack 5 and plugin ModuleFederation
https://www.npmjs.com/package/ngx-mfe
MIT License
22 stars 4 forks source link

OnChanges Not WORk #22

Closed alirezabahary closed 1 year ago

alirezabahary commented 1 year ago

When changing the inputs, the ngOnChanges method is not being called, and we must use a setTimeOut to delay fetching the data. Under no circumstances is the ngOnChanges method being invoked

dkhrunov commented 1 year ago

Hello, @alirezabahary

NgOnChanges is not invoked on the mfe component, when you change directive property "inputs", right?

dkhrunov commented 1 year ago

ngOnChanges is called when an Input Parameter is changed from your parent component.

This lib doesn't pass inputs from mfe directive direct to your mfe-components, so if you want to do something after any detection you should use ngAfterViewChecked()

dkhrunov commented 1 year ago

If you need pass data and some how to react t changes in this data, you can

If you need to pass data and be able to somehow react to data changes, you can do this:

  1. Then you can create a new Injector with context
    
    myData$ = new Subject<number>();

const myInjector = Injector.create({ parent: this._injector, providers: [ { provide: 'CONTEXT/FOR_MY_COMPONENT', useValue: { data: myData$ }, }, ] });

2.  Pass injector to mfeOutletDirective
```html
<ng-container *mfeOutlet="
    'my-component';
    module: 'MyModule';
    component: 'MyComponent';
    injector: myInjector;
 ">
</ng-container>
  1. And finally get this context from DI in your mfe-component
    class MyComponent {
    constructor(@Inject('CONTEXT/FOR_MY_COMPONENT') context: { data: Observable<number>} {
        context.data.subscribe((data) => {
            // do something 
            // fetch your additional data
        });
    }