且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

***问题:How to share CMSComponentData between several components in Spartacus manner

更新时间:2022-09-04 19:11:51

问题:https://***.com/questions/65815289/how-to-share-cmscomponentdata-between-several-components-in-spartacus-manner/65820581#65820581


Let’s imagine that we have CMS Component:


ConfigModule.withConfig({

 cmsComponents: {

   CMSSupportComponent: {

     component: SupportButtonComponent,

   },

 },

}),

1

2

3

4

5

6

7

But from inside we have to open another Angular component which also needs the same CMSComponentData. Simple Angular component relates to the same Angular module.


I know that we can do it via Angular Service, common for Angular manner (this requires some handy work). But maybe Spartacus has some approaches similar to:


ConfigModule.withConfig({

 cmsComponents: {

   SearchBoxComponent: {

       providers: [

       {

         provide: SearchBoxComponentService,

         useClass: CustomSearchBoxComponentService,

         deps: [CmsComponentData, ProductSearchService, RoutingService]

       }

     ];

   }

 }

});

1

2

3

4

5

6

7

8

9

10

11

12

13

and we can share injected CmsComponentData via single Service for several components.


解答

as defined in Spartacus documentation:


The CMS data that is related to a component is provided to the component by the CmsComponentData service during instantiation. The CmsComponentData service contains the component uid, and also data$, which is an observable to the component payload. By making use of the Angular dependency injection (DI) system, components and component-specific services can use the CmsComponentData.


it’s recommended way to use Angular DI to inject CmsComponentData service and used in your components.