Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 6cab8e1

Browse files
committed
add phonecat dual router prose
1 parent d8d6655 commit 6cab8e1

File tree

6 files changed

+87
-65
lines changed

6 files changed

+87
-65
lines changed

public/docs/_examples/upgrade-phonecat-3-router/ts/app/app-routing.module.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,19 @@ import { NgModule } from '@angular/core';
33
import { Routes, RouterModule, UrlHandlingStrategy, UrlTree } from '@angular/router';
44
import { APP_BASE_HREF, HashLocationStrategy, LocationStrategy } from '@angular/common';
55

6-
import { PhoneDetailComponent } from './phone-detail/phone-detail.component';
76
import { PhoneListComponent } from './phone-list/phone-list.component';
87

98
export class Ng1Ng2UrlHandlingStrategy implements UrlHandlingStrategy {
10-
// shouldProcessUrl(url: UrlTree) { return false; }
119
shouldProcessUrl(url: UrlTree) {
12-
return url.toString().startsWith('/phones')
13-
|| url.toString() === '/';
10+
return url.toString() === '/' || url.toString() === '/phones';
1411
}
1512
extract(url: UrlTree) { return url; }
1613
merge(url: UrlTree, whole: UrlTree) { return url; }
1714
}
1815

1916
const routes: Routes = [
2017
{ path: '', redirectTo: 'phones', pathMatch: 'full' },
21-
{ path: 'phones', component: PhoneListComponent },
22-
{ path: 'phones/:phoneId', component: PhoneDetailComponent }
18+
{ path: 'phones', component: PhoneListComponent }
2319
];
2420

2521
@NgModule({

public/docs/_examples/upgrade-phonecat-3-router/ts/app/app.config.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
angular.
44
module('phonecatApp').
55
config(['$locationProvider', '$routeProvider',
6-
function config($locationProvider: angular.ILocationProvider) {
6+
function config($locationProvider: angular.ILocationProvider,
7+
$routeProvider: angular.route.IRouteProvider) {
78
$locationProvider.hashPrefix('!');
9+
// #docregion ng1-routes
10+
$routeProvider
11+
.when('/phones/:phoneId', {
12+
template: '<phone-detail></phone-detail>'
13+
});
14+
// #enddocregion ng1-routes
815
}
916
]);

public/docs/_examples/upgrade-phonecat-3-router/ts/app/phone-detail/phone-detail.component.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
// #docplaster
12
// #docregion
23
declare var angular: angular.IAngularStatic;
34
import { downgradeComponent } from '@angular/upgrade/static';
4-
5-
import { Component } from '@angular/core';
6-
import { ActivatedRoute } from '@angular/router';
5+
import { Component, Inject } from '@angular/core';
76

87
import { Phone, PhoneData } from '../core/phone/phone.service';
98

@@ -15,12 +14,11 @@ export class PhoneDetailComponent {
1514
phone: PhoneData;
1615
mainImageUrl: string;
1716

18-
constructor(activatedRoute: ActivatedRoute, phone: Phone) {
19-
phone.get(activatedRoute.snapshot.params['phoneId'])
20-
.subscribe((p: PhoneData) => {
21-
this.phone = p;
22-
this.setImage(p.images[0]);
23-
});
17+
constructor(@Inject('$routeParams') $routeParams: any, phone: Phone) {
18+
phone.get($routeParams['phoneId']).subscribe(phone => {
19+
this.phone = phone;
20+
this.setImage(phone.images[0]);
21+
});
2422
}
2523

2624
setImage(imageUrl: string) {

public/docs/_examples/upgrade-phonecat-3-router/ts/app/phone-list/phone-list.template.html

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<div class="col-md-2">
44
<!--Sidebar content-->
55

6-
<!-- #docregion controls -->
76
<p>
87
Search:
98
<input [(ngModel)]="query" />
@@ -16,7 +15,6 @@
1615
<option value="age">Newest</option>
1716
</select>
1817
</p>
19-
<!-- #enddocregion controls -->
2018

2119
</div>
2220
<div class="col-md-10">

public/docs/_examples/upgrade-phonecat-3-router/ts/index.html

+2-8
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,13 @@
2525
<script src="phone-list/phone-list.module.js"></script>
2626
<script src="phone-detail/phone-detail.module.js"></script>
2727

28-
<!-- #docregion ng2 -->
2928
<script src="/node_modules/core-js/client/shim.min.js"></script>
3029
<script src="/node_modules/zone.js/dist/zone.min.js"></script>
3130
<script src="/node_modules/reflect-metadata/Reflect.js"></script>
3231
<script src="/node_modules/systemjs/dist/system.src.js"></script>
33-
<!-- #enddocregion ng2 -->
32+
3433
<script src="/systemjs.config.1.js"></script>
35-
<!-- #docregion ng2
36-
<script src="/systemjs.config.js"></script>
37-
<script>
38-
System.import('/app');
39-
</script>
40-
<!-- #enddocregion ng2 -->
34+
4135
<script>
4236
System.import('/app');
4337
</script>

public/docs/ts/latest/guide/upgrade.jade

+68-39
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ include ../_util-fns
4545
4. [Upgrading the Phone service](#upgrading-the-phone-service)
4646
5. [Upgrading Components](#upgrading-components)
4747
6. [AoT compile the hybrid app](#aot-compile-the-hybrid-app)
48-
7. [Switching To The Angular 2 Router And Bootstrap](#switching-to-the-angular-2-router-and-bootstrap)
48+
7. [Adding The Angular 2 Router And Bootstrap](#adding-the-angular-2-router-and-bootstrap)
4949
8. [Say Goodbye to Angular 1](#say-goodbye-to-angular-1)
5050
3. [Appendix: Upgrading PhoneCat Tests](#appendix-upgrading-phonecat-tests)
5151

@@ -1455,31 +1455,33 @@ code-example(format="").
14551455
And that's all you need to use AoT while upgrading your app!
14561456

14571457
:marked
1458-
## Switching To The Angular 2 Router And Bootstrap
1458+
## Adding The Angular 2 Router And Bootstrap
14591459

14601460
At this point we've replaced all Angular 1 application components with
1461-
their Angular 2 counterparts.
1462-
1463-
The application is still bootstrapped as a hybrid app.
1464-
There's no need for that anymore.
1465-
It's time to remove the last remnants of Angular 1 in two final steps:
1466-
1. Switch to the Angular 2 router.
1467-
1. Bootstrap as a pure Angular 2 app.
1461+
their Angular 2 counterparts, even though we're still serving them from the Angular 1 router.
1462+
1463+
Most Angular 1 apps have more than a couple of routes though, and it's very helpful to migrate
1464+
one route at a time.
1465+
1466+
Let's start by migrating the initial `/` and `/phones` routes to Angular 2,
1467+
while keeping `/phones/:phoneId` in the Angular 1 router.
14681468

1469-
### Switch to the Angular 2 router
1469+
1470+
### Add the Angular 2 router
14701471
Angular 2 has an [all-new router](router.html).
14711472

14721473
Like all routers, it needs a place in the UI to display routed views.
1473-
The Angular 2 that's the `<router-outlet>` and it belongs in a *root component*
1474+
For Angular 2 that's the `<router-outlet>` and it belongs in a *root component*
14741475
at the top of the applications component tree.
14751476

14761477
We don't yet have such a root component, because the app is still managed as an Angular 1 app.
14771478
Create a new `app.component.ts` file with the following `AppComponent` class:
14781479

1479-
+makeExample('upgrade-phonecat-4-final/ts/app/app.component.ts', null, 'app/app.component.ts')(format='.')
1480+
+makeExample('upgrade-phonecat-3-router/ts/app/app.component.ts', null, 'app/app.component.ts')(format='.')
14801481

14811482
:marked
1482-
It has a simple template that only includes the `<router-outlet>`.
1483+
It has a simple template that only includes the `<router-outlet>` for Angular 2 routes
1484+
and `ng-view` for Angular 1 routes.
14831485
This component just renders the contents of the active route and nothing else.
14841486

14851487
The selector tells Angular 2 to plug this root component into the `<phonecat-app>`
@@ -1488,65 +1490,80 @@ code-example(format="").
14881490
Add this `<phonecat-app>` element to the `index.html`.
14891491
It replaces the old Angular 1 `ng-view` directive:
14901492

1491-
+makeExample('upgrade-phonecat-4-final/ts/index.html', 'appcomponent', 'index.html (body)')(format='.')
1493+
+makeExample('upgrade-phonecat-3-router/ts/index.html', 'appcomponent', 'index.html (body)')(format='.')
14921494

14931495
:marked
14941496
### Create the _Routing Module_
14951497
A router needs configuration whether it's the Angular 1 or Angular 2 or any other router.
14961498

14971499
The details of Angular 2 router configuration are best left to the [Routing documentation](router.html)
14981500
which recommends that you create a `NgModule` dedicated to router configuration
1499-
(called a _Routing Module_):
1501+
(called a _Routing Module_).
15001502

1501-
+makeExample('upgrade-phonecat-4-final/ts/app/app-routing.module.ts', null, 'app/app-routing.module.ts')
1503+
+makeExample('upgrade-phonecat-3-router/ts/app/app-routing.module.ts', null, 'app/app-routing.module.ts')
15021504

15031505
:marked
1504-
This module defines a `routes` object with two routes to the two phone components
1506+
This module defines a `routes` object with one route to the phone list component
15051507
and a default route for the empty path.
15061508
It passes the `routes` to the `RouterModule.forRoot` method which does the rest.
15071509

1508-
A couple of extra providers enable routing with "hash" URLs such as `#!/phones` instead of the default "push state" strategy.
1510+
A couple of extra providers enable routing with "hash" URLs such as `#!/phones`
1511+
instead of the default "push state" strategy.
1512+
1513+
There's a twist to our Routing Module though: we're also adding a custom `UrlHandlingStrategy`
1514+
that tells the Angular 2 router to only process the `/` and `/phones` routes.
15091515

15101516
Now update the `AppModule` to import this `AppRoutingModule` and also the
1511-
declare the root `AppComponent`:
1517+
declare the root `AppComponent` as the bootstrap component.
1518+
That tells Angular 2 that it should bootstrap the app with the _root_ `AppComponent` and
1519+
insert it's view into the host web page.
15121520

1513-
+makeExample('upgrade-phonecat-4-final/ts/app/app.module.ts', null, 'app/app.module.ts')
1521+
We can also remove the `ngDoBootstrap()` override from `app.module.ts` since we are now
1522+
bootstrapping from Angular 2.
1523+
1524+
+makeExample('upgrade-phonecat-3-router/ts/app/app.module.ts', null, 'app/app.module.ts')
15141525

15151526
:marked
1516-
The Angular 2 router passes route parameters differently.
1517-
Correct the `PhoneDetail` component constructor to expect an injected `ActivatedRoute` object.
1518-
Extract the `phoneId` from the `ActivatedRoute.snapshot.params` and fetch the phone data as before:
1527+
Now we need to tell the Angular 1 router to only process the `/phones/:phoneId` route:
15191528

1520-
+makeExample('upgrade-phonecat-4-final/ts/app/phone-detail/phone-detail.component.ts', null, 'app/phone-detail/phone-detail.component.ts')
1529+
+makeExample('upgrade-phonecat-3-router/ts/app/app.config.ts', 'ng1-routes', 'app/app.config.ts (route config)')
15211530
:marked
15221531
### Generate links for each phone
15231532

15241533
We no longer have to hardcode the links to phone details in the phone list.
1525-
We can generate them data binding each phone's `id` to the `routerLink` directive
1534+
We can generate data bindings for each phone's `id` to the `routerLink` directive
15261535
and let that directive construct the appropriate URL to the `PhoneDetailComponent`:
15271536

1528-
+makeExample('upgrade-phonecat-4-final/ts/app/phone-list/phone-list.template.html', 'list', 'app/phone-list/phone-list.template.html (list with links)')(format='.')
1537+
+makeExample('upgrade-phonecat-3-router/ts/app/phone-list/phone-list.template.html', 'list', 'app/phone-list/phone-list.template.html (list with links)')(format='.')
15291538
.l-sub-section
15301539
:marked
15311540
See the [Routing](router.html) page for details.
15321541

15331542
:marked
1534-
### Bootstrap as an Angular 2 app
1543+
We are now running both routers at the same time!
1544+
Angular 2 is handling the initial `/` url, redirecting to `/phones`.
1545+
Meanwhile when we click a link to the phone detail, Angular 1 takes over.
1546+
1547+
This way we can incrementally upgrade our app, reducing the risk of a massive one step router
1548+
swap.
1549+
1550+
The next step is to migrate the `/phones/:phoneId` route.
15351551

1536-
You may have noticed one extra `bootstrap` metadata property added to the `AppModule`
1537-
+makeExample('upgrade-phonecat-4-final/ts/app/app.module.ts', 'bootstrap', 'app/app.module.ts (bootstrap)')(format='.')
15381552
:marked
1539-
That tells Angular 2 that it should bootstrap the app with the _root_ `AppComponent` and
1540-
insert it's view into the host web page.
1553+
The Angular 2 router passes route parameters differently.
1554+
Correct the `PhoneDetail` component constructor to expect an injected `ActivatedRoute` object.
1555+
Extract the `phoneId` from the `ActivatedRoute.snapshot.params` and fetch the phone data as before:
15411556

1542-
Now switch the bootstrap method of the application from the `UpgradeAdapter`
1543-
to the Angular 2 way.
1557+
+makeExample('upgrade-phonecat-4-final/ts/app/phone-detail/phone-detail.component.ts', null, 'app/phone-detail/phone-detail.component.ts')
15441558

1545-
Now we can drop `upgrade.bootstrap` from our application bootstrap, and remove the
1546-
`ngDoBootstrap()` override from `app.module.ts`
1559+
:marked
1560+
Since this was the last route we want to migrate over, we can also now delete the last
1561+
route config from `app/app.config.ts`, and add it to the Angular 2 router configuration.
1562+
1563+
We don't need our `UrlHandlingStrategy` anymore either, since now Angular 2 is processing all
1564+
routes.
15471565

1548-
+makeExample('upgrade-phonecat-4-final/ts/app/main.ts', null, 'main.ts')
1549-
+makeExample('upgrade-phonecat-4-final/ts/app/app.module.ts', null, 'app.module.ts')
1566+
+makeExample('upgrade-phonecat-4-final/ts/app/app-routing.module.ts', null, 'app/app-routing.module.ts')
15501567

15511568
:marked
15521569
You are now running a pure Angular 2 application!
@@ -1557,10 +1574,22 @@ code-example(format="").
15571574
its new life as a pure, shiny Angular 2 app. The remaining tasks all have to
15581575
do with removing code - which of course is every programmer's favorite task!
15591576

1577+
The application is still bootstrapped as a hybrid app.
1578+
There's no need for that anymore.
1579+
1580+
Switch the bootstrap method of the application from the `UpgradeAdapter`
1581+
to the Angular 2 way.
1582+
1583+
+makeExample('upgrade-phonecat-4-final/ts/app/main.ts', null, 'main.ts')
1584+
1585+
:marked
15601586
If you haven't already, remove all references to the `UpgradeModule` from `app.module.ts`,
15611587
as well as any [Factory provider](#making-angular-1-dependencies-injectable-to-angular-2) for Angular 1 services.
1562-
Also remove any `downgradeComponent()` you find, together with the associated Angular 1
1563-
directive declarations.
1588+
1589+
Also remove any `downgradeInjectable` or `downgradeComponent()` you find,
1590+
together with the associated Angular 1 factory or directive declarations.
1591+
1592+
+makeExample('upgrade-phonecat-4-final/ts/app/app.module.ts', null, 'app.module.ts')
15641593

15651594
:marked
15661595
You may also completely remove the following files. They are Angular 1

0 commit comments

Comments
 (0)