@@ -45,7 +45,7 @@ include ../_util-fns
45
45
4. [Upgrading the Phone service](#upgrading-the-phone-service)
46
46
5. [Upgrading Components](#upgrading-components)
47
47
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)
49
49
8. [Say Goodbye to Angular 1](#say-goodbye-to-angular-1)
50
50
3. [Appendix: Upgrading PhoneCat Tests](#appendix-upgrading-phonecat-tests)
51
51
@@ -1455,31 +1455,33 @@ code-example(format="").
1455
1455
And that's all you need to use AoT while upgrading your app!
1456
1456
1457
1457
:marked
1458
- ## Switching To The Angular 2 Router And Bootstrap
1458
+ ## Adding The Angular 2 Router And Bootstrap
1459
1459
1460
1460
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 .
1468
1468
1469
- ### Switch to the Angular 2 router
1469
+
1470
+ ### Add the Angular 2 router
1470
1471
Angular 2 has an [all-new router](router.html).
1471
1472
1472
1473
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*
1474
1475
at the top of the applications component tree.
1475
1476
1476
1477
We don't yet have such a root component, because the app is still managed as an Angular 1 app.
1477
1478
Create a new `app.component.ts` file with the following `AppComponent` class:
1478
1479
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 ='.' )
1480
1481
1481
1482
: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.
1483
1485
This component just renders the contents of the active route and nothing else.
1484
1486
1485
1487
The selector tells Angular 2 to plug this root component into the `<phonecat-app>`
@@ -1488,65 +1490,80 @@ code-example(format="").
1488
1490
Add this `<phonecat-app>` element to the `index.html`.
1489
1491
It replaces the old Angular 1 `ng-view` directive:
1490
1492
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 ='.' )
1492
1494
1493
1495
:marked
1494
1496
### Create the _Routing Module_
1495
1497
A router needs configuration whether it's the Angular 1 or Angular 2 or any other router.
1496
1498
1497
1499
The details of Angular 2 router configuration are best left to the [Routing documentation](router.html)
1498
1500
which recommends that you create a `NgModule` dedicated to router configuration
1499
- (called a _Routing Module_):
1501
+ (called a _Routing Module_).
1500
1502
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' )
1502
1504
1503
1505
: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
1505
1507
and a default route for the empty path.
1506
1508
It passes the `routes` to the `RouterModule.forRoot` method which does the rest.
1507
1509
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.
1509
1515
1510
1516
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.
1512
1520
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' )
1514
1525
1515
1526
: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:
1519
1528
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) ' )
1521
1530
:marked
1522
1531
### Generate links for each phone
1523
1532
1524
1533
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
1526
1535
and let that directive construct the appropriate URL to the `PhoneDetailComponent`:
1527
1536
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 ='.' )
1529
1538
.l-sub-section
1530
1539
:marked
1531
1540
See the [Routing](router.html) page for details.
1532
1541
1533
1542
: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.
1535
1551
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 ='.' )
1538
1552
: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:
1541
1556
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' )
1544
1558
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.
1547
1565
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' )
1550
1567
1551
1568
:marked
1552
1569
You are now running a pure Angular 2 application!
@@ -1557,10 +1574,22 @@ code-example(format="").
1557
1574
its new life as a pure, shiny Angular 2 app. The remaining tasks all have to
1558
1575
do with removing code - which of course is every programmer's favorite task!
1559
1576
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
1560
1586
If you haven't already, remove all references to the `UpgradeModule` from `app.module.ts`,
1561
1587
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' )
1564
1593
1565
1594
:marked
1566
1595
You may also completely remove the following files. They are Angular 1
0 commit comments