Angular 角度2可观测JWT头问题

Angular 角度2可观测JWT头问题,angular,angular2-observables,Angular,Angular2 Observables,我有个奇怪的问题。我需要有一个成功的登录,然后保存JWT,然后我需要用一个新的头发出另一个HTTP请求,其中包括JWT作为获取用户详细信息的授权。不幸的是,在我的第二个HTTP请求中,我错过了JWT。任何帮助都将不胜感激 以下是我的代码 API服务 import { Injectable } from '@angular/core'; import { Headers, Http, Response } from '@angular/http'; import { Observable } fr

我有个奇怪的问题。我需要有一个成功的登录,然后保存JWT,然后我需要用一个新的头发出另一个HTTP请求,其中包括JWT作为获取用户详细信息的授权。不幸的是,在我的第二个HTTP请求中,我错过了JWT。任何帮助都将不胜感激

以下是我的代码

API服务

import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import 'rxjs/add/observable/throw';
import { AppState } from './state.service';

@Injectable()
export class ApiService {
  token: string = this._appState.get('_token');

  headers: Headers = new Headers({
    'Content-Type': 'application/json',
    Accept: 'application/json',
    'Authorization': this.token ? this.token : null
  });

  api_url: string = '/';

  constructor(private http: Http, public _appState: AppState) {}

  private getJson(response: Response) {
    return response.json();
  }

  private checkForError(response: Response): Response {
    if (response.status >= 200 && response.status < 300) {
      return response;
    } else {
      let error = new Error(response.statusText);
      error['response'] = response;
      console.error(error);
      throw error;
    }
  }

  getall(path: string): Observable<any> {
    console.log('from api', this.token);
    return this.http.get(`${this.api_url}${path}`, { headers: this.headers })
    .map(this.checkForError)
    .catch(err => Observable.throw(err))
    .map(this.getJson);
  }

  get(path: string, query: string): Observable<any> {
    return this.http.get(`${this.api_url}${path}/${query}`, { headers: this.headers })
    .map(this.checkForError)
    .catch(err => Observable.throw(err))
    .map(this.getJson);
  }

  post(path: string, body): Observable<any> {
    return this.http.post(
      `${this.api_url}${path}`,
      JSON.stringify(body),
      { headers: this.headers }
    )
    .map(this.checkForError)
    .catch(err => Observable.throw(err))
    .map(this.getJson);
  }

  put(path: string, query: string, body): Observable<any> {
    return this.http.put(
        `${this.api_url}${path}/${query}`,
        JSON.stringify(body),
        { headers: this.headers }
      )
      .map(this.checkForError)
      .catch(err => Observable.throw(err))
      .map(this.getJson);
  }

  delete(path: string): Observable<any> {
    return this.http.delete(
      `${this.api_url}${path}`,
      { headers: this.headers }
    )
    .map(this.checkForError)
    .catch(err => Observable.throw(err))
    .map(this.getJson);
  }
}
登录服务

import { Injectable } from '@angular/core';
import { ApiService } from '../../services';
import 'rxjs/Rx';

@Injectable()
export class CredFormService {
  path: string = 'auth/';

  constructor( private _apiService: ApiService ) {

  }

  login(body) {
    return this._apiService.post(this.path + 'login', body);
  }

  signup(body) {
    return this._apiService.post(this.path + 'signup', body);
  }
}
用户服务

import { Injectable } from '@angular/core';
import { ApiService } from './restful.services';

@Injectable()
export class GetUserService {

  private _path: string = 'apis/user';

  constructor( private _apiService: ApiService ) { }

  getUser() {
    return this._apiService.getall(this._path);
  }

}
国家公务员

import { Injectable } from '@angular/core';

export type InternalStateType = {
  [key: string]: any
};

@Injectable()
export class AppState {
  _state: InternalStateType = { };

  constructor() {

  }

  // already return a clone of the current state
  get state() {
    return this._state = this._clone(this._state);
  }
  // never allow mutation
  set state(value) {
    throw new Error('do not mutate the `.state` directly');
  }


  get(prop?: any) {
    // use our state getter for the clone
    const state = this.state;
    return state.hasOwnProperty(prop) ? state[prop] : state;
  }

  set(prop: string, value: any) {
    // internally mutate our state
    return this._state[prop] = value;
  }


  private _clone(object: InternalStateType) {
    // simple object clone
    return JSON.parse(JSON.stringify( object ));
  }
}

在我的第二个请求中,JWT没有添加到标题中。

同样的问题,您找到解决方案了吗?:)是的,我错了。这是使用Rxjs的承诺回报。只需遵循方法..同样的问题在这里,你找到解决方案了吗?:)是的,我错了。这是使用Rxjs的承诺回报。按照这个方法去做就行了。。
import { Injectable } from '@angular/core';

export type InternalStateType = {
  [key: string]: any
};

@Injectable()
export class AppState {
  _state: InternalStateType = { };

  constructor() {

  }

  // already return a clone of the current state
  get state() {
    return this._state = this._clone(this._state);
  }
  // never allow mutation
  set state(value) {
    throw new Error('do not mutate the `.state` directly');
  }


  get(prop?: any) {
    // use our state getter for the clone
    const state = this.state;
    return state.hasOwnProperty(prop) ? state[prop] : state;
  }

  set(prop: string, value: any) {
    // internally mutate our state
    return this._state[prop] = value;
  }


  private _clone(object: InternalStateType) {
    // simple object clone
    return JSON.parse(JSON.stringify( object ));
  }
}