Skip to content

Commit 3a36527

Browse files
routing module
1 parent 4e1c4f8 commit 3a36527

File tree

6 files changed

+97
-52
lines changed

6 files changed

+97
-52
lines changed

package-lock.json

Lines changed: 33 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@
1212
},
1313
"private": true,
1414
"dependencies": {
15-
"@angular/animations": "^5.0.0",
16-
"@angular/common": "^5.0.0",
17-
"@angular/compiler": "^5.0.0",
18-
"@angular/core": "^5.0.0",
19-
"@angular/forms": "^5.0.0",
20-
"@angular/http": "^5.0.0",
21-
"@angular/platform-browser": "^5.0.0",
22-
"@angular/platform-browser-dynamic": "^5.0.0",
23-
"@angular/router": "^5.0.0",
15+
"@angular/animations": "^5.0.3",
16+
"@angular/common": "^5.0.3",
17+
"@angular/compiler": "^5.0.3",
18+
"@angular/core": "^5.0.3",
19+
"@angular/forms": "^5.0.3",
20+
"@angular/http": "^5.0.3",
21+
"@angular/platform-browser": "^5.0.3",
22+
"@angular/platform-browser-dynamic": "^5.0.3",
23+
"@angular/router": "^5.0.3",
2424
"core-js": "^2.4.1",
2525
"rxjs": "^5.5.2",
2626
"zone.js": "^0.8.14"
2727
},
2828
"devDependencies": {
2929
"@angular/cli": "1.5.2",
30-
"@angular/compiler-cli": "^5.0.0",
31-
"@angular/language-service": "^5.0.0",
30+
"@angular/compiler-cli": "^5.0.3",
31+
"@angular/language-service": "^5.0.3",
3232
"@types/jasmine": "~2.5.53",
3333
"@types/jasminewd2": "~2.0.2",
3434
"@types/node": "~6.0.60",

src/app/app-routing.module.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { LoginComponent } from './login/login.component';
55
import { DashboardComponent } from './dashboard/dashboard.component';
66

77
import { AuthService } from './services/auth.service';
8+
import { ConfigService } from './services/config.service';
89

910
const routes: Routes = [
1011
{
@@ -14,21 +15,24 @@ const routes: Routes = [
1415
},
1516
{
1617
path: 'login',
17-
component: LoginComponent
18+
component: LoginComponent,
19+
resolve: {
20+
configs: ConfigService
21+
}
1822
},
1923
{
2024
path: 'dashboard',
2125
component: DashboardComponent,
22-
canActivate: [ AuthService ]
26+
canActivate: [ AuthService ],
27+
resolve: {
28+
configs: ConfigService
29+
}
2330
}
2431
];
2532

2633
@NgModule({
27-
imports: [
28-
RouterModule.forRoot(routes)
29-
],
30-
exports: [
31-
RouterModule
32-
]
34+
imports: [RouterModule.forRoot(routes)],
35+
exports: [RouterModule],
36+
providers: [ConfigService]
3337
})
3438
export class AppRoutingModule { }

src/app/login/login.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export class LoginComponent implements OnInit {
2929

3030
ngOnInit() {
3131

32+
// dump config data
33+
// console.log(this.route.snapshot.data.configs);
34+
3235
// logout current user
3336
this.authService.logout();
3437
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/dashboard';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { TestBed, inject } from '@angular/core/testing';
2+
3+
import { ConfigService } from './config.service';
4+
5+
describe('ConfigService', () => {
6+
beforeEach(() => {
7+
TestBed.configureTestingModule({
8+
providers: [ConfigService]
9+
});
10+
});
11+
12+
it('should be created', inject([ConfigService], (service: ConfigService) => {
13+
expect(service).toBeTruthy();
14+
}));
15+
});

src/app/services/config.service.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Injectable } from '@angular/core';
2+
import { Resolve, ActivatedRouteSnapshot } from '@angular/router';
3+
4+
import { environment } from '../../environments/environment';
5+
6+
@Injectable()
7+
export class ConfigService implements Resolve<any> {
8+
9+
resolve(route: ActivatedRouteSnapshot) {
10+
return new Promise((resolve, reject) => {
11+
12+
// get local configuration file based on environment
13+
const env = environment.production ? 'prod' : 'dev';
14+
const filePath = './../../configs/' + env + '.json';
15+
16+
// read json file internally here and pass json data
17+
// to below resolve function
18+
resolve({
19+
'test': 'sample json data'
20+
});
21+
});
22+
}
23+
}

0 commit comments

Comments
 (0)