From 068263c9f8c69dab6116ef82ff334ee437fb8346 Mon Sep 17 00:00:00 2001 From: Tim Deschryver Date: Sat, 23 Jun 2018 16:34:45 +0200 Subject: [PATCH] refactor(example): refactor routes as routing modules (#1139) --- .../app/{routes.ts => app-routing.module.ts} | 9 +++++++- example-app/app/app.module.ts | 8 +++---- example-app/app/auth/auth-routing.module.ts | 11 ++++++++++ example-app/app/auth/auth.module.ts | 4 ++-- example-app/app/books/books-routing.module.ts | 22 +++++++++++++++++++ example-app/app/books/books.module.ts | 12 ++-------- 6 files changed, 48 insertions(+), 18 deletions(-) rename example-app/app/{routes.ts => app-routing.module.ts} (63%) create mode 100644 example-app/app/auth/auth-routing.module.ts create mode 100644 example-app/app/books/books-routing.module.ts diff --git a/example-app/app/routes.ts b/example-app/app/app-routing.module.ts similarity index 63% rename from example-app/app/routes.ts rename to example-app/app/app-routing.module.ts index e5a3fcb1b6..1d4bb424bf 100644 --- a/example-app/app/routes.ts +++ b/example-app/app/app-routing.module.ts @@ -1,4 +1,5 @@ -import { Routes } from '@angular/router'; +import { NgModule } from '@angular/core'; +import { Routes, RouterModule } from '@angular/router'; import { AuthGuard } from './auth/services/auth-guard.service'; import { NotFoundPageComponent } from './core/containers/not-found-page.component'; @@ -11,3 +12,9 @@ export const routes: Routes = [ }, { path: '**', component: NotFoundPageComponent }, ]; + +@NgModule({ + imports: [RouterModule.forRoot(routes, { useHash: true })], + exports: [RouterModule], +}) +export class AppRoutingModule {} diff --git a/example-app/app/app.module.ts b/example-app/app/app.module.ts index 1a1a8805d8..2bdd50f21a 100644 --- a/example-app/app/app.module.ts +++ b/example-app/app/app.module.ts @@ -3,7 +3,6 @@ import { CommonModule } from '@angular/common'; import { HttpClientModule } from '@angular/common/http'; import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; -import { RouterModule } from '@angular/router'; import { StoreModule } from '@ngrx/store'; import { EffectsModule } from '@ngrx/effects'; @@ -17,13 +16,13 @@ import { StoreDevtoolsModule } from '@ngrx/store-devtools'; import { CoreModule } from './core/core.module'; import { AuthModule } from './auth/auth.module'; -import { routes } from './routes'; import { reducers, metaReducers } from './reducers'; import { schema } from './db'; import { CustomRouterStateSerializer } from './shared/utils'; import { AppComponent } from './core/containers/app.component'; import { environment } from '../environments/environment'; +import { AppRoutingModule } from './app-routing.module'; @NgModule({ imports: [ @@ -31,7 +30,8 @@ import { environment } from '../environments/environment'; BrowserModule, BrowserAnimationsModule, HttpClientModule, - RouterModule.forRoot(routes, { useHash: true }), + AuthModule.forRoot(), + AppRoutingModule, /** * StoreModule.forRoot is imported once in the root module, accepting a reducer @@ -84,8 +84,6 @@ import { environment } from '../environments/environment'; DBModule.provideDB(schema), CoreModule.forRoot(), - - AuthModule.forRoot(), ], providers: [ /** diff --git a/example-app/app/auth/auth-routing.module.ts b/example-app/app/auth/auth-routing.module.ts new file mode 100644 index 0000000000..1c59e41014 --- /dev/null +++ b/example-app/app/auth/auth-routing.module.ts @@ -0,0 +1,11 @@ +import { NgModule } from '@angular/core'; +import { Routes, RouterModule } from '@angular/router'; +import { LoginPageComponent } from './containers/login-page.component'; + +const routes: Routes = [{ path: 'login', component: LoginPageComponent }]; + +@NgModule({ + imports: [RouterModule.forChild(routes)], + exports: [RouterModule], +}) +export class AuthRoutingModule {} diff --git a/example-app/app/auth/auth.module.ts b/example-app/app/auth/auth.module.ts index d4ac462489..990790e363 100644 --- a/example-app/app/auth/auth.module.ts +++ b/example-app/app/auth/auth.module.ts @@ -1,6 +1,5 @@ import { NgModule, ModuleWithProviders } from '@angular/core'; import { CommonModule } from '@angular/common'; -import { RouterModule } from '@angular/router'; import { ReactiveFormsModule } from '@angular/forms'; import { StoreModule } from '@ngrx/store'; import { EffectsModule } from '@ngrx/effects'; @@ -12,6 +11,7 @@ import { AuthGuard } from './services/auth-guard.service'; import { AuthEffects } from './effects/auth.effects'; import { reducers } from './reducers'; import { MaterialModule } from '../material'; +import { AuthRoutingModule } from './auth-routing.module'; export const COMPONENTS = [LoginPageComponent, LoginFormComponent]; @@ -32,7 +32,7 @@ export class AuthModule { @NgModule({ imports: [ AuthModule, - RouterModule.forChild([{ path: 'login', component: LoginPageComponent }]), + AuthRoutingModule, StoreModule.forFeature('auth', reducers), EffectsModule.forFeature([AuthEffects]), ], diff --git a/example-app/app/books/books-routing.module.ts b/example-app/app/books/books-routing.module.ts new file mode 100644 index 0000000000..c1c4ce5ea7 --- /dev/null +++ b/example-app/app/books/books-routing.module.ts @@ -0,0 +1,22 @@ +import { NgModule } from '@angular/core'; +import { Routes, RouterModule } from '@angular/router'; +import { FindBookPageComponent } from './containers/find-book-page.component'; +import { ViewBookPageComponent } from './containers/view-book-page.component'; +import { CollectionPageComponent } from './containers/collection-page.component'; +import { BookExistsGuard } from './guards/book-exists.guard'; + +export const routes: Routes = [ + { path: 'find', component: FindBookPageComponent }, + { + path: ':id', + component: ViewBookPageComponent, + canActivate: [BookExistsGuard], + }, + { path: '', component: CollectionPageComponent }, +]; + +@NgModule({ + imports: [RouterModule.forChild(routes)], + exports: [RouterModule], +}) +export class BooksRoutingModule {} diff --git a/example-app/app/books/books.module.ts b/example-app/app/books/books.module.ts index b2b265366b..3640361309 100644 --- a/example-app/app/books/books.module.ts +++ b/example-app/app/books/books.module.ts @@ -1,6 +1,5 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; -import { RouterModule } from '@angular/router'; import { StoreModule } from '@ngrx/store'; import { EffectsModule } from '@ngrx/effects'; @@ -16,21 +15,14 @@ import { CollectionPageComponent } from './containers/collection-page.component' import { MaterialModule } from '../material'; import { reducers } from './reducers'; +import { BooksRoutingModule } from './books-routing.module'; @NgModule({ imports: [ CommonModule, MaterialModule, ComponentsModule, - RouterModule.forChild([ - { path: 'find', component: FindBookPageComponent }, - { - path: ':id', - component: ViewBookPageComponent, - canActivate: [BookExistsGuard], - }, - { path: '', component: CollectionPageComponent }, - ]), + BooksRoutingModule, /** * StoreModule.forFeature is used for composing state