'ConfigModule.withConfig or provideConfig in Spartacus

What is the difference between those two configs and which should be used when?

ConfigModule.withConfig({})

and

provideConfig({})

The documentation didn't provide the clear answer to this question. https://sap.github.io/spartacus-docs/global-configuration-in-spartacus/



Solution 1:[1]

As mentioned in the docs, they are the same.

ConfigModule.withConfig({}) behaves the same way as provideConfig({})

The difference between them is that the first is used to be used in import's array, whereas the latter is used in the provider's array

However, you can create an issue in the Spartacus repository https://github.com/SAP/spartacus/issues in order to open the discussion of deprecating ConfigModule.withConfig

'legacy' way

@NgModule({
  imports: [ConfigModule.withConfig({...})],
  providers: [...]
  ...
})
...

'preferred' way

@NgModule({
  imports: [...],
  providers: [provideConfig({...})]
  ...
})

Solution 2:[2]

Full examples:

old:

import {CmsConfig, ConfigModule} from "@spartacus/core";

@NgModule({
  imports: [
    CommonModule,
    ConfigModule.withConfig({
      cmsComponents:{
        YourCustomComponentFlexType: {
          component: YourCustomComponent
        }
      }
    } as CmsConfig),
  ],
})

or preferred one:

import {provideConfig} from "@spartacus/core";

@NgModule({
  imports: [
    CommonModule
  ],
  providers: [
    provideConfig({
      cmsComponents: {
        YourCustomComponentFlexType: {
          component: YourCustomComponent
        }
      }
    }),
  ]
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BrianGJ
Solution 2