0

i make a small angular application and convert it into angular element to use it as a micro frontend in other master App and it looks great and works fine. but I need to pass a value from Master App to the Micro App but didn't work with me. can anyone help with this issue.

Micro Application app.module.ts

export class AppModule {

  constructor(private injector: Injector) { }

    ngDoBootstrap(){
      const element = createCustomElement(AppComponent, {injector: this.injector});
      customElements.define('micro-fe', element);
    }
}

Micro Application app.component.ts

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'Micro-FE';
  @Input() public myName: string = "";

  ngOnInit() {
      console.log(this.myName);
    }
}

Micro Application app.component.html

<h1>Hello From Micro-Frontend !!!!!</h1>
<h2>My Name : {{myName}}</h2>

and in the Master Application index.html

<!doctype html>
<html lang="en">
<body>
  <app-root></app-root>

  <micro-fe myName="Amr"></micro-fe>
  <script src="assets/Micro-FE/main.js"></script>
  <script src="assets/Micro-FE/polyfills.js"></script>
  <script src="assets/Micro-FE/runtime.js"></script>
  
</body>
</html>
6
  • I’d expect this to work actually..? What happens when you put an ngOnchanges in the WC with a console? Commented Aug 21, 2022 at 12:33
  • @MikeOne where to put the ngOnchanges on Micro App or Master App ? Commented Aug 21, 2022 at 12:43
  • Two angular applications cannot communicate between one another natively, you can do it by sending events to the window from app A (window.dispatchEvent()), and listening to the events on app B (fromEvent(window, '...').subscribe()). Here is an article Commented Aug 21, 2022 at 14:29
  • Wait.. it changes the mapping. Use <micro-fe my-name="Amr"> Commented Aug 21, 2022 at 16:41
  • @MikeOne thanks Mike it works fine with me, but what is the good explanation for this change, from myName -> my-name Commented Aug 22, 2022 at 6:50

1 Answer 1

0

You need to use property binding and quotes with string. I don't see any other reason why it wouldn't work.

Try <micro-fe [myName]="'Amr'"></micro-fe>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.