diff --git a/modules/router-store/spec/integration.spec.ts b/modules/router-store/spec/integration.spec.ts index 15473d2aaa..11254d38c0 100644 --- a/modules/router-store/spec/integration.spec.ts +++ b/modules/router-store/spec/integration.spec.ts @@ -1,4 +1,4 @@ -import { Component, Provider } from '@angular/core'; +import { Component, Provider, Injectable } from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { NavigationEnd, Router, RouterStateSnapshot } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; @@ -416,7 +416,11 @@ describe('integration spec', () => { : null; }; + @Injectable() class CustomSerializer implements RouterStateSerializer { + constructor(store: Store) { + // Requiring store to test Serializer with injected arguments works. + } serialize(routerState: RouterStateSnapshot): SerializedState { const url = `${routerState.url}-custom`; const params = { test: 1 }; diff --git a/modules/router-store/src/index.ts b/modules/router-store/src/index.ts index 90dff3b0b3..ec721bc920 100644 --- a/modules/router-store/src/index.ts +++ b/modules/router-store/src/index.ts @@ -19,7 +19,6 @@ export { RouterNavigatedPayload, StoreRouterConnectingModule, StoreRouterConfig, - StoreRouterConfigFunction, NavigationActionTiming, ROUTER_CONFIG, DEFAULT_ROUTER_FEATURENAME, diff --git a/modules/router-store/src/router_store_module.ts b/modules/router-store/src/router_store_module.ts index c213871bba..d3b3d328a7 100644 --- a/modules/router-store/src/router_store_module.ts +++ b/modules/router-store/src/router_store_module.ts @@ -175,7 +175,7 @@ export function routerReducer< export interface StoreRouterConfig { stateKey?: string; - serializer?: new () => RouterStateSerializer; + serializer?: new (...args: any[]) => RouterStateSerializer; /** * By default, ROUTER_NAVIGATION is dispatched before guards and resolvers run. * Therefore, the action could run too soon, for example @@ -200,34 +200,16 @@ export const ROUTER_CONFIG = new InjectionToken( export const DEFAULT_ROUTER_FEATURENAME = 'router'; export function _createRouterConfig( - config: StoreRouterConfig | StoreRouterConfigFunction + config: StoreRouterConfig ): StoreRouterConfig { - let _config: StoreRouterConfig; - - if (typeof config === 'function') { - _config = config(); - } else { - _config = config || {}; - } - return { stateKey: DEFAULT_ROUTER_FEATURENAME, serializer: DefaultRouterStateSerializer, navigationActionTiming: NavigationActionTiming.PreActivation, - ..._config, + ...config, }; } -export function _createSerializer( - config: StoreRouterConfig -): RouterStateSerializer { - // This function gets handed a complete config-object from _createRouterConfig, - // so we know the serializer property exists - return new config.serializer!(); -} - -export type StoreRouterConfigFunction = () => StoreRouterConfig; - enum RouterTrigger { NONE = 1, ROUTER = 2, @@ -280,10 +262,7 @@ enum RouterTrigger { providers: [ { provide: _ROUTER_CONFIG, - useValue: { - stateKey: DEFAULT_ROUTER_FEATURENAME, - serializer: DefaultRouterStateSerializer, - }, + useValue: {}, }, { provide: ROUTER_CONFIG, @@ -292,21 +271,23 @@ enum RouterTrigger { }, { provide: RouterStateSerializer, - deps: [ROUTER_CONFIG], - useFactory: _createSerializer, + useClass: DefaultRouterStateSerializer, }, ], }) export class StoreRouterConnectingModule { - static forRoot( - config?: StoreRouterConfig | StoreRouterConfigFunction - ): ModuleWithProviders; - static forRoot( - config: StoreRouterConfig | StoreRouterConfigFunction = {} - ): ModuleWithProviders { + static forRoot(config: StoreRouterConfig = {}): ModuleWithProviders { return { ngModule: StoreRouterConnectingModule, - providers: [{ provide: _ROUTER_CONFIG, useValue: config }], + providers: [ + { provide: _ROUTER_CONFIG, useValue: config }, + { + provide: RouterStateSerializer, + useClass: config.serializer + ? config.serializer + : DefaultRouterStateSerializer, + }, + ], }; }