在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML5  HTML/ angualr4 使用resolve守衛(wèi)先向服務(wù)器請求數(shù)據(jù),在將數(shù)據(jù)到給組件,可

angualr4 使用resolve守衛(wèi)先向服務(wù)器請求數(shù)據(jù),在將數(shù)據(jù)到給組件,可是帶給組件的數(shù)據(jù)卻為String類型,怎么辦?

resolve守衛(wèi)
export class Getcasedata implements Resolve<CaseInfo> {

private getcasedata= {};
constructor(private http: HttpInterceptorService, private router: Router) {}
login() {

return this.http.request({
  method: 'get',
  url: '../../assets/data/caseInfo.json',
  data: {
    id: 11,
  },
});

}
resolve (route: ActivatedRouteSnapshot ): CaseInfo {
let id: string = route.queryParams['id'];

return this.login();

}
}

http服務(wù);
public get(url: string, params: any): any {

return this.http.get(url, {search: params})
  .toPromise()
  .then(this.handleSuccess)
  .catch(res => this.handleError(res));

}

/**

    • post請求
    • @param url 接口地址
    • @param params 參數(shù)
    • @returns {Promise<R>|Promise<U>}
      */
    1. post(url: string, params: any) {
      return this.http.post(url, params)

      .toPromise()
      .then(this.handleSuccess)
      .catch(res => this.handleError(res));

      }

      /**

      • 處理請求成功
      • @param res
      • @returns {{data: (string|null|((node:any)=>any)
        */
    2. handleSuccess(res: Response) {
      let body: {} = res['_body'];
      if (body) {

      return {
        data: body || {},
        statusText: res.statusText,
        status: res.status,
        success: true
      };

      }
      else {

      return {
        statusText: res.statusText,
        status: res.status,
        success: true
      };

      }

      }

    組件接收數(shù)據(jù)

    this.routerinfo.data.subscribe((data: {caseInfo: CaseInfo}) => {
      let id: string = this.routerinfo.queryParams['id'];
      this.caseData = data.caseInfo;
      console.log( typeof  this.caseData);
    });
    
    回答
    編輯回答
    陌南塵

    看了你的代碼 你是自己寫了handleSuccess函數(shù)對數(shù)據(jù)進(jìn)行了處理
    但是感覺似乎并沒有很大必要,不如直接使用 Promise 或者 Observable

    似乎是因?yàn)樯倭?
    res => res.json()
    導(dǎo)致返回是字符串的

    小小看法 希望對你有所幫助

    2017年6月15日 18:00
    編輯回答
    若相惜

    問題出在這里,let body: {} = res['_body']

    你可以在控制臺輸出這個(gè)body,是包括響應(yīng)頭信息等各種數(shù)據(jù)。

    你期望返回的數(shù)據(jù)還需要添加body.json()

    2018年6月23日 06:16