Is there a way in Angular2 to retrieve GET parameters and store them locally similar to how sessions are handled in PHP?
I need to obtain the access_token before navigating to the Dashboard component, which makes secure REST Webservice calls that require the token.
@Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<nav>
<a [routerLink]="['Dashboard']">Dashboard</a>
<a [routerLink]="['Heroes']">Heroes</a>
</nav>
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES],
providers: [
ROUTER_PROVIDERS,
HTTP_PROVIDERS,
HeroService,
RouteParams
]
})
@RouteConfig([
{
path: '/heroes',
name: 'Heroes',
component: HeroesComponent
},
{
path: '/dashboard',
name: 'Dashboard',
component: DashboardComponent,
useAsDefault: true
},
{
path: '/getHero/:id',
name: 'HeroDetail',
component: HeroDetailComponent
},
])
export class AppComponent {
title = 'Tour of Heroes';
private token2;
constructor(
private _routeParams:RouteParams) {
this.token2 = _routeParams.get('access_token');
console.log("token from Url : "+ this.token2);
}
}
Upon launching the app, an "EXCEPTION: Cannot resolve all parameters for 'RouteParams'(?)" error is encountered.
hero.service.ts :
@Injectable()
export class HeroService {
ot: Observable<string>;
private token2 = 'test';
private serviceUrl = 'http://localhost:8080/Context_Path/';
private token = "xXXXX";
private headers = new Headers();
constructor(private http:Http){
this.headers.append('Authorization', 'bearer '+ this.token);
this.headers.append('Content-Type', 'application/json');
}
// More code here...
Dashborad.component.ts :
import { Component, OnInit } from 'angular2/core';
import { Hero } from './hero';
import { HeroService } from './hero.service';
import { Router } from 'angular2/router';
// More code here...
Errors persist even after attempting relevant edits. Troubleshooting continues to resolve issues related to 'RouteParams'.