角度解析器无法访问角度服务属性

时间:2018-07-16 05:32:09

标签: javascript typescript angular5

我有一个名为authservice的综合服务,如下所示:

  import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import { JwtHelperService} from '@auth0/angular-jwt';


@Injectable()
export class AuthService {
   public DecdedToken: any;
   userToken: any;
  public  userid: any;
      helper: any = new JwtHelperService();
    baseUrl = 'http://localhost:5000/api/auth/';
    // userToken: any;
 constructor(private http: Http , private jwthelpservicee: JwtHelperService) {}
    login(model: any) {
      return this.http.post(this.baseUrl + 'login', model, this.requestOptions()).map((response: Response) => {
          const user = response.json();
          if (user && user.stringToken) {
              this.userToken = user.stringToken;
              localStorage.setItem('token', user.stringToken);
              this.DecdedToken = this.helper.decodeToken(user.stringToken);
              this.userid = this.DecdedToken.nameid;
             // console.log(this.userid);
               // onsole.log('so far so good');
          }
      }).catch(this.HandleError);
    }

    register(model: any) {
     return this.http.post(this.baseUrl + 'register', model, this.requestOptions()).catch(this.HandleError);
    }
    private requestOptions() {
      const headers = new Headers({ 'Content-type': 'application/json' });
      return new RequestOptions({ headers: headers });
    }
    IsLoggedIn() {
      return !this.jwthelpservicee.isTokenExpired();
  }
    private HandleError(error: any) {
      const applicationerror = error.headers.get('Application-Error');
      if (applicationerror) {
        return Observable.throw(applicationerror);

      }
      const serverError = error.json();
      let modelStateErrors = '';
      if (serverError) {
        for (const key in serverError) {
          if (serverError[key]) {
            modelStateErrors += serverError[key] + '\n';
          }
        }
      }
     return Observable.throw(modelStateErrors || 'server error');
    }
  }
登录方法DecodedToken中的

获得其值。 我也有一个解析器,我正在尝试从authsercvice获取属性的值

import { Resolve, Router, ActivatedRouteSnapshot } from '../../../node_modules/@angular/router';
import { User } from '../_Models/user';
import { Injectable } from '../../../node_modules/@angular/core';
import { UserService } from '../_services/User.service';
import { AlertifyService } from '../_services/alertify.service';
import { Observable } from '../../../node_modules/rxjs';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';
import { AuthService } from '../_services/auth.service';
@Injectable()
export class MemberEditResolver implements Resolve<User> {

    constructor(private userservice: UserService ,
         private router: Router ,
          private alertify: AlertifyService,
          private authservice: AuthService) {

    }
    resolve(route: ActivatedRouteSnapshot): Observable<User> {
        console.log(this.authservice.userid);
     return   this.userservice.getuser(this.authservice.DecdedToken.nameid).catch(error => {

            this.alertify.error('error');
            this.router.navigate(['/members']);
            return Observable.of(null);
        });
    }
}

如您所见,我已将DecdedToken定义为一个公共属性,在authservice内此属性具有值,但是当我想从解析器访问它时,我得到null或未定义...

0 个答案:

没有答案