@@ -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
@@ -1457,31 +1457,33 @@ code-example(format="").
1457
1457
And that's all you need to use AoT while upgrading your app!
1458
1458
1459
1459
:marked
1460
- ### Switching To The Angular 2 Router And Bootstrap
1460
+ ### Adding The Angular 2 Router And Bootstrap
1461
1461
1462
1462
At this point we've replaced all Angular 1 application components with
1463
- their Angular 2 counterparts.
1464
-
1465
- The application is still bootstrapped as a hybrid app.
1466
- There's no need for that anymore.
1467
- It's time to remove the last remnants of Angular 1 in two final steps:
1468
- 1. Switch to the Angular 2 router.
1469
- 1. Bootstrap as a pure Angular 2 app.
1463
+ their Angular 2 counterparts, even though we're still serving them from the Angular 1 router.
1464
+
1465
+ Most Angular 1 apps have more than a couple of routes though, and it's very helpful to migrate
1466
+ one route at a time.
1467
+
1468
+ Let's start by migrating the initial `/` and `/phones` routes to Angular 2,
1469
+ while keeping `/phones/:phoneId` in the Angular 1 router.
1470
+
1471
+ #### Add the Angular 2 router
1470
1472
1471
- #### Switch to the Angular 2 router
1472
1473
Angular 2 has an [all-new router](router.html).
1473
1474
1474
1475
Like all routers, it needs a place in the UI to display routed views.
1475
- The Angular 2 that's the `<router-outlet>` and it belongs in a *root component*
1476
+ For Angular 2 that's the `<router-outlet>` and it belongs in a *root component*
1476
1477
at the top of the applications component tree.
1477
1478
1478
1479
We don't yet have such a root component, because the app is still managed as an Angular 1 app.
1479
1480
Create a new `app.component.ts` file with the following `AppComponent` class:
1480
1481
1481
- + makeExample('upgrade-phonecat-4-final /ts/app/app.component.ts' , null , 'app/app.component.ts' )( format ='.' )
1482
+ + makeExample('upgrade-phonecat-3-router /ts/app/app.component.ts' , null , 'app/app.component.ts' )( format ='.' )
1482
1483
1483
1484
:marked
1484
- It has a simple template that only includes the `<router-outlet>`.
1485
+ It has a simple template that only includes the `<router-outlet>` for Angular 2 routes
1486
+ and `ng-view` for Angular 1 routes.
1485
1487
This component just renders the contents of the active route and nothing else.
1486
1488
1487
1489
The selector tells Angular 2 to plug this root component into the `<phonecat-app>`
@@ -1490,65 +1492,80 @@ code-example(format="").
1490
1492
Add this `<phonecat-app>` element to the `index.html`.
1491
1493
It replaces the old Angular 1 `ng-view` directive:
1492
1494
1493
- + makeExample('upgrade-phonecat-4-final /ts/index.html' , 'appcomponent' , 'index.html (body)' )( format ='.' )
1495
+ + makeExample('upgrade-phonecat-3-router /ts/index.html' , 'appcomponent' , 'index.html (body)' )( format ='.' )
1494
1496
1495
1497
:marked
1496
1498
#### Create the _Routing Module_
1497
1499
A router needs configuration whether it's the Angular 1 or Angular 2 or any other router.
1498
1500
1499
1501
The details of Angular 2 router configuration are best left to the [Routing documentation](router.html)
1500
1502
which recommends that you create a `NgModule` dedicated to router configuration
1501
- (called a _Routing Module_):
1503
+ (called a _Routing Module_).
1502
1504
1503
- + makeExample('upgrade-phonecat-4-final /ts/app/app-routing.module.ts' , null , 'app/app-routing.module.ts' )
1505
+ + makeExample('upgrade-phonecat-3-router /ts/app/app-routing.module.ts' , null , 'app/app-routing.module.ts' )
1504
1506
1505
1507
:marked
1506
- This module defines a `routes` object with two routes to the two phone components
1508
+ This module defines a `routes` object with one route to the phone list component
1507
1509
and a default route for the empty path.
1508
1510
It passes the `routes` to the `RouterModule.forRoot` method which does the rest.
1509
1511
1510
- A couple of extra providers enable routing with "hash" URLs such as `#!/phones` instead of the default "push state" strategy.
1512
+ A couple of extra providers enable routing with "hash" URLs such as `#!/phones`
1513
+ instead of the default "push state" strategy.
1514
+
1515
+ There's a twist to our Routing Module though: we're also adding a custom `UrlHandlingStrategy`
1516
+ that tells the Angular 2 router to only process the `/` and `/phones` routes.
1511
1517
1512
1518
Now update the `AppModule` to import this `AppRoutingModule` and also the
1513
- declare the root `AppComponent`:
1519
+ declare the root `AppComponent` as the bootstrap component.
1520
+ That tells Angular 2 that it should bootstrap the app with the _root_ `AppComponent` and
1521
+ insert it's view into the host web page.
1514
1522
1515
- + makeExample('upgrade-phonecat-4-final/ts/app/app.module.ts' , null , 'app/app.module.ts' )
1523
+ We can also remove the `ngDoBootstrap()` override from `app.module.ts` since we are now
1524
+ bootstrapping from Angular 2.
1525
+
1526
+ + makeExample('upgrade-phonecat-3-router/ts/app/app.module.ts' , null , 'app/app.module.ts' )
1516
1527
1517
1528
:marked
1518
- The Angular 2 router passes route parameters differently.
1519
- Correct the `PhoneDetail` component constructor to expect an injected `ActivatedRoute` object.
1520
- Extract the `phoneId` from the `ActivatedRoute.snapshot.params` and fetch the phone data as before:
1529
+ Now we need to tell the Angular 1 router to only process the `/phones/:phoneId` route:
1521
1530
1522
- + makeExample('upgrade-phonecat-4-final /ts/app/phone-detail/phone-detail.component .ts' , null , 'app/phone-detail/phone-detail.component .ts' )
1531
+ + makeExample('upgrade-phonecat-3-router /ts/app/app.config .ts' , 'ng1-routes' , 'app/app.config .ts (route config) ' )
1523
1532
:marked
1524
1533
#### Generate links for each phone
1525
1534
1526
1535
We no longer have to hardcode the links to phone details in the phone list.
1527
- We can generate them data binding each phone's `id` to the `routerLink` directive
1536
+ We can generate data bindings for each phone's `id` to the `routerLink` directive
1528
1537
and let that directive construct the appropriate URL to the `PhoneDetailComponent`:
1529
1538
1530
- + 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 ='.' )
1539
+ + 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 ='.' )
1531
1540
.l-sub-section
1532
1541
:marked
1533
1542
See the [Routing](router.html) page for details.
1534
1543
1535
1544
:marked
1536
- #### Bootstrap as an Angular 2 app
1545
+ We are now running both routers at the same time!
1546
+ Angular 2 is handling the initial `/` url, redirecting to `/phones`.
1547
+ Meanwhile when we click a link to the phone detail, Angular 1 takes over.
1548
+
1549
+ This way we can incrementally upgrade our app, reducing the risk of a massive one step router
1550
+ swap.
1551
+
1552
+ The next step is to migrate the `/phones/:phoneId` route.
1537
1553
1538
- You may have noticed one extra `bootstrap` metadata property added to the `AppModule`
1539
- + makeExample('upgrade-phonecat-4-final/ts/app/app.module.ts' , 'bootstrap' , 'app/app.module.ts (bootstrap)' )( format ='.' )
1540
1554
:marked
1541
- That tells Angular 2 that it should bootstrap the app with the _root_ `AppComponent` and
1542
- insert it's view into the host web page.
1555
+ The Angular 2 router passes route parameters differently.
1556
+ Correct the `PhoneDetail` component constructor to expect an injected `ActivatedRoute` object.
1557
+ Extract the `phoneId` from the `ActivatedRoute.snapshot.params` and fetch the phone data as before:
1543
1558
1544
- Now switch the bootstrap method of the application from the `UpgradeAdapter`
1545
- to the Angular 2 way.
1559
+ + makeExample('upgrade-phonecat-4-final/ts/app/phone-detail/phone-detail.component.ts' , null , 'app/phone-detail/phone-detail.component.ts' )
1546
1560
1547
- Now we can drop `upgrade.bootstrap` from our application bootstrap, and remove the
1548
- `ngDoBootstrap()` override from `app.module.ts`
1561
+ :marked
1562
+ Since this was the last route we want to migrate over, we can also now delete the last
1563
+ route config from `app/app.config.ts`, and add it to the Angular 2 router configuration.
1564
+
1565
+ We don't need our `UrlHandlingStrategy` anymore either, since now Angular 2 is processing all
1566
+ routes.
1549
1567
1550
- + makeExample('upgrade-phonecat-4-final/ts/app/main.ts' , null , 'main.ts' )
1551
- + makeExample('upgrade-phonecat-4-final/ts/app/app.module.ts' , null , 'app.module.ts' )
1568
+ + makeExample('upgrade-phonecat-4-final/ts/app/app-routing.module.ts' , null , 'app/app-routing.module.ts' )
1552
1569
1553
1570
:marked
1554
1571
You are now running a pure Angular 2 application!
@@ -1559,10 +1576,22 @@ code-example(format="").
1559
1576
its new life as a pure, shiny Angular 2 app. The remaining tasks all have to
1560
1577
do with removing code - which of course is every programmer's favorite task!
1561
1578
1579
+ The application is still bootstrapped as a hybrid app.
1580
+ There's no need for that anymore.
1581
+
1582
+ Switch the bootstrap method of the application from the `UpgradeAdapter`
1583
+ to the Angular 2 way.
1584
+
1585
+ + makeExample('upgrade-phonecat-4-final/ts/app/main.ts' , null , 'main.ts' )
1586
+
1587
+ :marked
1562
1588
If you haven't already, remove all references to the `UpgradeModule` from `app.module.ts`,
1563
1589
as well as any [Factory provider](#making-angular-1-dependencies-injectable-to-angular-2) for Angular 1 services.
1564
- Also remove any `downgradeComponent()` you find, together with the associated Angular 1
1565
- directive declarations.
1590
+
1591
+ Also remove any `downgradeInjectable` or `downgradeComponent()` you find,
1592
+ together with the associated Angular 1 factory or directive declarations.
1593
+
1594
+ + makeExample('upgrade-phonecat-4-final/ts/app/app.module.ts' , null , 'app.module.ts' )
1566
1595
1567
1596
:marked
1568
1597
You may also completely remove the following files. They are Angular 1
0 commit comments