Skip to content
This repository was archived by the owner on Sep 21, 2018. It is now read-only.

Commit 4b62db6

Browse files
feat(router): Replaced @ngrx/router with @angular/router
1 parent 8a965e6 commit 4b62db6

13 files changed

Lines changed: 109 additions & 64 deletions

File tree

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
Example application utilizing @ngrx libraries, showcasing common patterns and best practices. You can find the live app [here](http://ngrx.github.io/example-app/).
44

5-
This app is a book collection manager. Using the Google Books API, the user can search for books and add them to their collection. This application utilizes [@ngrx/db](https://github.com/ngrx/db) to persist the collection across sessions; [@ngrx/store](https://github.com/ngrx/store) to manage the state of the app and to cache requests made to the Google Books API; [@ngrx/router](https://github.com/ngrx/router) to manage navigation between routes; and [@ngrx/effects](https://github.com/ngrx/effects) to isolate side effects.
5+
This app is a book collection manager. Using the Google Books API, the user can search for books and add them to their collection. This application utilizes [@ngrx/db](https://github.com/ngrx/db) to persist the collection across sessions; [@ngrx/store](https://github.com/ngrx/store) to manage the state of the app and to cache requests made to the Google Books API; [@angular/router](https://github.com/angular/angular) to manage navigation between routes; and [@ngrx/effects](https://github.com/ngrx/effects) to isolate side effects.
66

77
### Included
88
- [ngrx/store](https://github.com/ngrx/store) - RxJS powered state management for Angular2 apps, inspired by Redux
99
- [ngrx/effects](https://github.com/ngrx/effects) - Side effect model for @ngrx/store
10-
- [ngrx/router](https://github.com/ngrx/router) - Reactive Routing for Angular 2
10+
- [angular/router](https://github.com/angular/angular) - Angular2 Component Router
1111
- [ngrx/db](https://github.com/ngrx/db) - RxJS powered IndexedDB for Angular2 apps
12-
- [ngrx/router-store](https://github.com/ngrx/router-store) - Bindings to connect ngrx/router to ngrx/store
1312
- [ngrx/store-devtools](https://github.com/ngrx/store-devtools) - Instrumentation for @ngrx/store enabling time-travel debugging
1413
- [ngrx/store-log-monitor](https://github.com/ngrx/store-log-monitor) - A port of redux-devtools-log-monitor for Angular 2 and @ngrx/store
1514

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@angular/http": "2.0.0-rc.4",
3535
"@angular/platform-browser": "2.0.0-rc.4",
3636
"@angular/platform-browser-dynamic": "2.0.0-rc.4",
37+
"@angular/router": "3.0.0-beta.2",
3738
"@angular2-material/button": "2.0.0-alpha.6",
3839
"@angular2-material/card": "2.0.0-alpha.6",
3940
"@angular2-material/core": "2.0.0-alpha.6",
@@ -45,8 +46,6 @@
4546
"@ngrx/core": "^1.0.0",
4647
"@ngrx/db": "^1.1.0",
4748
"@ngrx/effects": "^1.0.1",
48-
"@ngrx/router": "^1.0.0-beta.1",
49-
"@ngrx/router-store": "0.0.1",
5049
"@ngrx/store": "^2.0.0",
5150
"@ngrx/store-devtools": "^2.0.0-beta.1",
5251
"@ngrx/store-log-monitor": "^2.0.0-beta.1",

src/app.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ import { StoreLogMonitorComponent } from '@ngrx/store-log-monitor';
4141
<md-sidenav-layout fullscreen>
4242
<md-sidenav #sidenav>
4343
<md-nav-list>
44-
<a md-list-item linkTo="/" (click)="sidenav.close()">
44+
<a md-list-item routerLink="/" (click)="sidenav.close()">
4545
<md-icon md-list-icon>book</md-icon>
4646
<span md-line>My Collection</span>
4747
<span md-line class="secondary">View your book collection!</span>
4848
</a>
49-
<a md-list-item linkTo="/book/find" (click)="sidenav.close()">
49+
<a md-list-item routerLink="/book/find" (click)="sidenav.close()">
5050
<md-icon md-list-icon>search</md-icon>
5151
<span md-line>Browse Books</span>
5252
<span md-line class="secondary">Find your next book!</span>
@@ -60,7 +60,7 @@ import { StoreLogMonitorComponent } from '@ngrx/store-log-monitor';
6060
<span>Books Sample App</span>
6161
</md-toolbar>
6262
63-
<route-view></route-view>
63+
<router-outlet></router-outlet>
6464
6565
</md-sidenav-layout>
6666
<ngrx-store-log-monitor toggleCommand="ctrl-t"></ngrx-store-log-monitor>

src/components/book-preview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type BookInput = Book;
1717
MD_LIST_DIRECTIVES
1818
],
1919
template: `
20-
<a [linkTo]=" '/book/' + id">
20+
<a [routerLink]="['/book', id]">
2121
<md-card>
2222
<md-card-title-group>
2323
<md-card-title>{{ title }}</md-card-title>

src/guards/book-exists.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'rxjs/add/operator/first';
33
import 'rxjs/add/observable/concat';
44
import { Injectable } from '@angular/core';
55
import { Store } from '@ngrx/store';
6-
import { Guard, TraversalCandidate } from '@ngrx/router';
6+
import { Router, CanActivate, ActivatedRouteSnapshot } from '@angular/router';
77
import { Observable } from 'rxjs/Observable';
88

99
import { GoogleBooksService } from '../services/google-books';
@@ -13,18 +13,16 @@ import { BookActions } from '../actions/book';
1313

1414
/**
1515
* Guards are hooks into the route resolution process, providing an opportunity
16-
* to inform the router's traversal process whether the route should continue
17-
* to be considered a candidate route. Guards must return an observable of
18-
* true or false.
19-
*
20-
* More on guards: https://github.com/ngrx/router/blob/master/docs/overview/guards.md
16+
* to inform the router's navigation process whether the route should continue
17+
* to activate this route. Guards must return an observable of true or false.
2118
*/
2219
@Injectable()
23-
export class BookExistsGuard implements Guard {
20+
export class BookExistsGuard implements CanActivate {
2421
constructor(
2522
private store: Store<AppState>,
2623
private googleBooks: GoogleBooksService,
27-
private bookActions: BookActions
24+
private bookActions: BookActions,
25+
private router: Router
2826
) { }
2927

3028
/**
@@ -55,7 +53,10 @@ export class BookExistsGuard implements Guard {
5553
.map(book => this.bookActions.loadBook(book))
5654
.do(action => this.store.dispatch(action))
5755
.map(book => !!book)
58-
.catch(() => Observable.of(false));
56+
.catch(() => {
57+
this.router.navigate(['/404']);
58+
return Observable.of(false);
59+
});
5960
}
6061

6162
/**
@@ -87,8 +88,8 @@ export class BookExistsGuard implements Guard {
8788
* on to the next candidate route. In this case, it will move on
8889
* to the 404 page.
8990
*/
90-
protectRoute({ routeParams: { id } }: TraversalCandidate) {
91+
canActivate(route: ActivatedRouteSnapshot) {
9192
return this.waitForCollectionToLoad()
92-
.switchMapTo(this.hasBook(id));
93+
.switchMapTo(this.hasBook(route.params['id']));
9394
}
9495
}

src/guards/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
export { BookExistsGuard } from './book-exists';
1+
import { BookExistsGuard } from './book-exists';
2+
3+
export { BookExistsGuard };
4+
5+
export default [
6+
BookExistsGuard
7+
];

src/main.browser.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { bootstrap } from '@angular/platform-browser-dynamic';
2-
import { HashLocationStrategy } from '@angular/common';
2+
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
33
import { disableDeprecatedForms, provideForms } from '@angular/forms';
4-
import { provideRouter } from '@ngrx/router';
4+
import { provideRouter, ROUTER_DIRECTIVES } from '@angular/router';
5+
import { PLATFORM_DIRECTIVES } from '@angular/core';
56
import { provideStore } from '@ngrx/store';
67
import { provideDB } from '@ngrx/db';
7-
import { connectRouterToStore } from '@ngrx/router-store';
88
import { runEffects } from '@ngrx/effects';
99
import { instrumentStore } from '@ngrx/store-devtools';
1010
import { useLogMonitor } from '@ngrx/store-log-monitor';
@@ -16,6 +16,7 @@ import reducer from './reducers';
1616
import effects from './effects';
1717
import services from './services';
1818
import actions from './actions';
19+
import guards from './guards';
1920

2021

2122
bootstrap(App, [
@@ -42,20 +43,21 @@ bootstrap(App, [
4243
runEffects(effects),
4344

4445
/**
45-
* provideRouter sets up all of the providers for @ngrx/router. It accepts
46+
* provideRouter sets up all of the providers for @angular/router. It accepts
4647
* an array of routes and a location strategy. By default, it will use
4748
* `PathLocationStrategy`.
48-
*
49-
* Source: https://github.com/ngrx/router/blob/master/lib/index.ts#L44-L51
5049
*/
51-
provideRouter(routes, HashLocationStrategy),
50+
provideRouter(routes),
51+
52+
/**
53+
* Make router directives available to all components
54+
*/
55+
{ provide: PLATFORM_DIRECTIVES, useValue: [ROUTER_DIRECTIVES], multi: true },
5256

5357
/**
54-
* connectRouterToStore configures additional providers that synchronize
55-
* router state with @ngrx/store. This lets you debug router state using
56-
* ngrx/store and to change the location by dispatching actions.
58+
* Override the default location strategy with `HashLocationStrategy`
5759
*/
58-
connectRouterToStore(),
60+
{ provide: LocationStrategy, useClass: HashLocationStrategy },
5961

6062
/**
6163
* provideDB sets up @ngrx/db with the provided schema and makes the Database
@@ -79,6 +81,7 @@ bootstrap(App, [
7981
*/
8082
services,
8183
actions,
84+
guards,
8285

8386
disableDeprecatedForms(),
8487
provideForms()

src/pages/book-view.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import '@ngrx/core/add/operator/select';
2+
import 'rxjs/add/operator/map';
23
import 'rxjs/add/operator/switchMap';
34
import { Component } from '@angular/core';
4-
import { RouteParams } from '@ngrx/router';
5+
import { ActivatedRoute } from '@angular/router';
56
import { Store } from '@ngrx/store';
67
import { Observable } from 'rxjs/Observable';
78

@@ -35,13 +36,15 @@ export class BookViewPage {
3536
constructor(
3637
private store: Store<AppState>,
3738
private bookActions: BookActions,
38-
private routeParams$: RouteParams
39+
private route: ActivatedRoute
3940
) {
40-
this.book$ = routeParams$
41+
this.book$ = route
42+
.params
4143
.select<string>('id')
4244
.switchMap(id => store.let(getBook(id)));
4345

44-
this.isBookInCollection$ = routeParams$
46+
this.isBookInCollection$ = route
47+
.params
4548
.select<string>('id')
4649
.switchMap(id => store.let(isBookInCollection(id)));
4750
}

src/pages/not-found.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { MdButton } from '@angular2-material/button';
1313
<p>Hey! It looks like this page doesn't exist yet.</p>
1414
</md-card-content>
1515
<md-card-actions>
16-
<button md-raised-button color="primary" linkTo="/">Take Me Home</button>
16+
<button md-raised-button color="primary" routerLink="/">Take Me Home</button>
1717
</md-card-actions>
1818
</md-card>
1919
`,

src/reducers/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Observable } from 'rxjs/Observable';
88
* it any number of functions and it returns a function. This new function
99
* takes a value and chains it through every composed function, returning
1010
* the output.
11-
*
11+
*
1212
* More: https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch5.html
1313
*/
1414
import { compose } from '@ngrx/core/compose';
@@ -28,17 +28,11 @@ import { storeLogger } from 'ngrx-store-logger';
2828
* functions and creates a new reducer that stores the gathers the values
2929
* of each reducer and stores them using the reducer's key. Think of it
3030
* almost like a database, where every reducer is a table in the db.
31-
*
31+
*
3232
* More: https://egghead.io/lessons/javascript-redux-implementing-combinereducers-from-scratch
3333
*/
3434
import { combineReducers } from '@ngrx/store';
3535

36-
/**
37-
* @ngrx/router-store keeps the router in sync with @ngrx/store. To connect the
38-
* two, we need to use the routerReducer.
39-
*/
40-
import { routerReducer, RouterState } from '@ngrx/router-store';
41-
4236

4337
/**
4438
* Every reducer module's default export is the reducer function itself. In
@@ -56,7 +50,6 @@ import collectionReducer, * as fromCollection from './collection';
5650
* our top level state interface is just a map of keys to inner state types.
5751
*/
5852
export interface AppState {
59-
router: RouterState;
6053
search: fromSearch.SearchState;
6154
books: fromBooks.BooksState;
6255
collection: fromCollection.CollectionState;
@@ -71,7 +64,6 @@ export interface AppState {
7164
* the result from right to left.
7265
*/
7366
export default compose(storeLogger(), combineReducers)({
74-
router: routerReducer,
7567
search: searchReducer,
7668
books: booksReducer,
7769
collection: collectionReducer

0 commit comments

Comments
 (0)