How do I share data between components in Angular 2?

后端 未结 6 598
故里飘歌
故里飘歌 2020-11-22 05:04

In Angular 1.x.x you simply ask for the same service and you end up with the same instance, making it possible to share the data in the service.

Now in Angular 2 I h

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 05:33

    It depends, if there is a simple case

    a) A -> B -> C A has two child B and C and if you want to share data between A and B or A and C then use (input / output)

    If you want to share between B and C then also you can use (input / output) but it is suggested to use Service.

    b) If the tree is big and complex. (if there are so many levels of parent and children connections.) And in this case if you want to share data then I would suggest ngrx

    It implements the flux architecture which creates a client side store to which any component can subscribe and can update without creating any race condition.

提交回复
热议问题