How SAP Spartacus default routing configuration works

Time:2021-10-15

The name of the default configuration file is default-routing-config.ts

How SAP Spartacus default routing configuration works

To see where defaultroutingconfig is consumed:

How SAP Spartacus default routing configuration works

Provudeddefaultconfig is a helper function:

How SAP Spartacus default routing configuration works

When will the routingmodule. Forroot method be called?

How SAP Spartacus default routing configuration works

In these two places: base-storefront.module.ts and storefront-foundation.module.ts:

How SAP Spartacus default routing configuration works

Note the type of the parameter returned by the forroot method: modulewithproviders:
How SAP Spartacus default routing configuration works

Because the consumption module of these modules is imported, the type of the imports area isany[] | Type<any> | ModuleWithProviders<{}>)[]

How SAP Spartacus default routing configuration works
How SAP Spartacus default routing configuration works

Description of valueprovider parameters:

How SAP Spartacus default routing configuration works

  • Provide: inject token. It is usually an instance of type or injectiontoken, but it can also be an any instance.
  • Multi: if true, the injector returns an array of instances. This is useful for allowing multiple providers to spread across multiple files to provide configuration information to a common token.

An example:

const locale = new InjectionToken<string[]>('locale');
const injector = Injector.create({
  providers: [
    {provide: locale, multi: true, useValue: 'en'},
    {provide: locale, multi: true, useValue: 'sk'},
  ]
});

const locales: string[] = injector.get(locale);
expect(locales).toEqual(['en', 'sk']);

Modulewithproviders: packaging of ngmodule and its related providers.

More Jerry’s original articles are: “Wang Zixi”:
How SAP Spartacus default routing configuration works