|
| 1 | +import { |
| 2 | + NgModule, |
| 3 | + Optional, |
| 4 | + SkipSelf, |
| 5 | + NO_ERRORS_SCHEMA, |
| 6 | + ModuleWithProviders |
| 7 | +} from '@angular/core'; |
| 8 | +import { NodeGuiLocationStrategy } from './location-strategy'; |
| 9 | +import { LocationStrategy, PlatformLocation } from '@angular/common'; |
| 10 | +import { NodeguiPlatformLocation } from './platform-location'; |
| 11 | +import { RouterModule, Routes, ExtraOptions } from '@angular/router'; |
| 12 | + |
| 13 | +const NS_ROUTER_PROVIDERS = [ |
| 14 | + { |
| 15 | + provide: NodeGuiLocationStrategy, |
| 16 | + useFactory: provideLocationStrategy, |
| 17 | + deps: [[NodeGuiLocationStrategy, new Optional(), new SkipSelf()]] |
| 18 | + }, |
| 19 | + { provide: LocationStrategy, useExisting: NodeGuiLocationStrategy }, |
| 20 | + NodeguiPlatformLocation, |
| 21 | + { provide: PlatformLocation, useExisting: NodeguiPlatformLocation } |
| 22 | +]; |
| 23 | + |
| 24 | +@NgModule({ |
| 25 | + imports: [RouterModule], |
| 26 | + exports: [RouterModule], |
| 27 | + schemas: [NO_ERRORS_SCHEMA] |
| 28 | +}) |
| 29 | +export class NodeguiRouterModule { |
| 30 | + static forRoot( |
| 31 | + routes: Routes, |
| 32 | + config?: ExtraOptions |
| 33 | + ): ModuleWithProviders<NodeguiRouterModule> { |
| 34 | + return { |
| 35 | + ngModule: NodeguiRouterModule, |
| 36 | + providers: [ |
| 37 | + ...RouterModule.forRoot(routes, config).providers, |
| 38 | + ...NS_ROUTER_PROVIDERS |
| 39 | + ] |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + static forChild(routes: Routes): ModuleWithProviders<NodeguiRouterModule> { |
| 44 | + return { |
| 45 | + ngModule: NodeguiRouterModule, |
| 46 | + providers: RouterModule.forChild(routes).providers |
| 47 | + }; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export function provideLocationStrategy( |
| 52 | + locationStrategy: NodeGuiLocationStrategy |
| 53 | +): NodeGuiLocationStrategy { |
| 54 | + return locationStrategy ? locationStrategy : new NodeGuiLocationStrategy(); |
| 55 | +} |
0 commit comments