我需要預裝一些URL數據解析來自http.get()的數據,所以 我已經解析:不能與角2.0.0
import {Injectable} from '@angular/core';
import {Resolve, ActivatedRouteSnapshot, RouterStateSnapshot, } from '@angular/router';
import {Observable} from 'rxjs/Rx';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class ProfileResolver implements Resolve<any> {
constructor(
private _http: Http
) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<any>|any {
return this._http.get('/session').map(data => {
console.log(data);
return data;
});
}
}
和組件:
import {Component} from '@angular/core';
import {RouterModule, ActivatedRoute} from '@angular/router';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
@Component({
selector: '[profile]',
host: {
class: 'profile-page app'
},
template: require('./profile.html')
})
export class Profile {
constructor(
private route:ActivatedRoute
) {
this.route.data.subscribe(data => {
console.log(data);
});
}
profile;
}
路由配置:
{
path: 'profile',
loadChildren:() => System.import('../profile/profile.module'),
resolve: {
profile: ProfileResolver
},
},
Console.log
在解析器顯示接收到的數據,但在成分是空着的OBJ等等,最新錯誤?當我在rc4中使用這個代碼時,一切都很好。 此外,如果我將return _http.get(...)
更改爲簡單值,例如return "123";
此代碼將起作用。提前致謝。
我發現我的數據'this.route.snapshot._resolve。 parent.resolvedData',但是得到它的正確方法是什麼? – Ivan
對於'http.get()'的記錄也會產生一個'Response'對象,你應該調用'.json()'或'.text()'來獲得響應數據。 –