Skip to content

Commit cf0814b

Browse files
committed
Part 3: Add Angular Router With Config
1 parent e15a66b commit cf0814b

11 files changed

+188
-17
lines changed

guide/background.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Background
2+
3+
Could have used https://github.com/ui-router/angular-hybrid but we wanted to use the 1st party router and move away from the 3rd party ui router
4+
5+
## Let's get into it
6+
7+
[Part 1: Set Up Angular and AngularJs Hybrid App](./part-1.md)

guide/overview.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,29 @@ Have a read through the official [Angular Upgrade Docs](https://angular.io/guide
44

55
At the end of this guide you should be able to set up a hybrid Angular 6+ and AngularJs (Angular 1.x) app with Angular Router and AngularJs UI-Router working nicely together.
66

7-
## 0. Scaffold New Project With Angular CLI
7+
Each part has an accociated commit in this repo. See [repo commits](https://github.com/BBlackwo/angular-router-angular-js-ui-router-hybrid/commits/master).
88

9-
```sh
10-
npx @angular/cli new your-project-name
11-
```
9+
## [Background](./background.md)
1210

13-
## 1. Set Up Angular and AngularJs Hybrid App
11+
## [Part 1: Set Up Angular and AngularJs Hybrid App](./part-1.md)
1412

15-
See [Part 1](./part-1.md)
13+
## [Part 2: Add UI Router With Config](./part-2.md)
1614

17-
## 2. Add UI Router With Config
15+
## [Part 3: Add Angular Router With Config](./part-3.md)
1816

19-
See [Part 2](./part-2.md)
17+
## [Part 4: Hacks To Get Hybrid Routing Working](./part-4.md)
2018

21-
## 3. Add Angular Router With Config
19+
## Conclusion
2220

2321
// ...
2422

25-
## 4. Some Fun Hacks
26-
27-
// ...
28-
29-
Generate router service with Angular CLI...
30-
3123
## TODO
3224

3325
- Generate TOC with <https://github.com/jonschlinkert/markdown-toc>
3426
- Note about legacy js files
3527
- Upgrade to Angular 7
28+
- Lazy loading route modules?
29+
- Background
30+
- Conclusion
31+
- Comment on http://www.syntaxsuccess.com/viewarticle/ngupgrade-with-the-angular-router-and-ui-router
32+
- What about all my js files that don't get transpiled?

guide/part-3.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# 3. Add Angular Router With Config
2+
3+
## Generate HelloNg Component
4+
5+
```sh
6+
npx ng generate component HelloNg
7+
```
8+
9+
## Generate and update AppRoutingModule
10+
11+
Note: If you initialise an Angular CLI project with the `--routing` flag this will automatically be created for you, so skip this step.
12+
13+
```sh
14+
npx ng generate module routing/app-routing --module=app --flat
15+
```
16+
17+
Once generated, add routes and RouerModule imports and exports.
18+
19+
[app.routing.module.ts](../src/app/routing/app.routing.module.ts)
20+
21+
```ts
22+
const routes: Routes = [];
23+
24+
@NgModule({
25+
imports: [RouterModule.forRoot(routes)],
26+
exports: [RouterModule]
27+
})
28+
export class AppRoutingModule { }
29+
```
30+
31+
## Configure Routes
32+
33+
[app.routing.module.ts](../src/app/routing/app.routing.module.ts)
34+
35+
```ts
36+
const routes: Routes = [
37+
{
38+
path: 'hello-ng',
39+
component: HelloNgComponent
40+
}
41+
];
42+
```
43+
44+
## Add RouterOutlet to AppComponent template
45+
46+
[app.component.html](../src/app/app.component.html)
47+
48+
```html
49+
<router-outlet></router-outlet>
50+
```
51+
52+
## Problem 1. Navigating to Angular Route Doesnt Work
53+
54+
Navigate to [/hello-ng](http://localhost:4200/hello-ng).
55+
56+
Now, the route doesn't load as you would expect. This is where we have to start hacking.
57+
58+
## Debugging
59+
60+
If you want to debug the routing you can update your `AppRoutingModule` as follows:
61+
62+
[app-routing.module.ts](../src/app/routing/app-routing.module.ts)
63+
64+
```ts
65+
imports: [RouterModule.forRoot(routes,
66+
{ enableTracing: true } // <-- For debugging purposes only. Should be turned off in prod.
67+
)],
68+
```
69+
70+
We can force the Angular Router to kick in by adding a `routerLink` for our new Angular route.
71+
72+
[app.component.html](../src/app/app.component.html)
73+
74+
```html
75+
<nav>
76+
<a routerLink="/hello-ng">Hello Ng</a>
77+
</nav>
78+
```
79+
80+
If you click that link it should work and you should see some logging in the console. But if you refresh the page the route will not load. The presence of AngularJs UI router is causing Angular router not to work. To fix that, go to the next step.
81+
82+
## Next step
83+
84+
[Part 4: Hacks To Get Hybrid Routing Working](./part-4.md)

src/app/app.component.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<div class="comp ng">
22
<h2>{{ title }}</h2>
3+
<nav>
4+
<a routerLink="/hello-ng">Hello Ng</a>
5+
</nav>
6+
<router-outlet></router-outlet>
37

48
<legacy-app-upgrade></legacy-app-upgrade>
59
</div>

src/app/app.module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ import { AppComponent } from './app.component';
66
import { LegacyAppUpgradeDirective } from './legacy/upgrade/legacy.app.component.upgrade';
77
import './legacy/'; // imported so legacy files are bundled
88
import { legacyApp } from './legacy/legacy.app.module';
9+
import { HelloNgComponent } from './hello-ng/hello-ng.component';
10+
import { AppRoutingModule } from './routing/app-routing.module';
911

1012
@NgModule({
1113
declarations: [
1214
AppComponent,
1315
// Import the upgraded legacyApp so Angular knows about it
1416
// and we can use it in our app component
15-
LegacyAppUpgradeDirective
17+
LegacyAppUpgradeDirective,
18+
HelloNgComponent
1619
],
1720
imports: [
1821
BrowserModule,
19-
UpgradeModule // Add this so Angular can use the Upgrade module
22+
// Add this so Angular can use the Upgrade module
23+
UpgradeModule,
24+
AppRoutingModule,
2025
],
2126
providers: [],
2227
// VERY IMPORTANT to change this from `bootstrap` to `entryComponents`!

src/app/hello-ng/hello-ng.component.css

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>
2+
hello-ng works!
3+
</p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { HelloNgComponent } from './hello-ng.component';
4+
5+
describe('HelloNgComponent', () => {
6+
let component: HelloNgComponent;
7+
let fixture: ComponentFixture<HelloNgComponent>;
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [ HelloNgComponent ]
12+
})
13+
.compileComponents();
14+
}));
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(HelloNgComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Component, OnInit } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-hello-ng',
5+
templateUrl: './hello-ng.component.html',
6+
styleUrls: ['./hello-ng.component.css']
7+
})
8+
export class HelloNgComponent implements OnInit {
9+
10+
constructor() { }
11+
12+
ngOnInit() {
13+
}
14+
15+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { AppRoutingModule } from './app-routing.module';
2+
3+
describe('AppRoutingModule', () => {
4+
let appRoutingModule: AppRoutingModule;
5+
6+
beforeEach(() => {
7+
appRoutingModule = new AppRoutingModule();
8+
});
9+
10+
it('should create an instance', () => {
11+
expect(appRoutingModule).toBeTruthy();
12+
});
13+
});

0 commit comments

Comments
 (0)