I'm using a Guard on an Angular application to resolve initial critical data. On the version 4 of Angular I was duing it like this:
// app.routing.ts
routing = [{
path: '', component: AppComponent, canActivate: [ResolveGuard],
}];
// resolve.guard.ts
@Injectable()
export class ResolveGuard implements CanActivate {
constructor(
private _api: ApiService,
) { }
canActivate(): any {
return this._api.apiGet('my/url').map(response) => {
if ( response.status === 'success') {
// Consume data here
return true;
}
return false;
}).first();
}
}
Since the new version of Http on Angular 5 doesn't use the .map()
property anymore, this is not working.
If I change .map()
to .subscribe()
it doesn't throw any errors, but the application never resolve properly. On the other hand, using .first()
and/or .map()
throw some errors, as expected in this version.
What should I do in this case?
I need to activate that route only if and when the initial data is loaded.
Edit to add info about the apiGet
function:
constructor(private _http: HttpClient) {}
public apiGet(url: string): any {
return this._http
.get(this.apiUrl + url)
.catch(this.handleError.bind(this));
}