Angular 2 - 在服务之间发送值
Angular 2 - Send value between services
我正在尝试使 canActivate
函数 returns true
或 false
.
布尔值将来自文件 login.service.ts
中的函数 signin()
在 response.json().message
中出现 true
或 false
但当我在文件 auth.guard.ts
.[= 中的 canActivate
函数中使用 this.loginService.logado()
时没有出现值24=]
有人知道如何解决这个问题吗?
login.service.ts
import {Injectable} from "@angular/core";
import {Http, Headers} from "@angular/http";
import {Observable} from "rxjs/Observable";
import 'rxjs/Rx';
import {Config} from '../config';
@Injectable()
export class LoginService{
token: string;
userId: string;
constructor (private _http: Http) {}
login(contentLogin){
const body = JSON.stringify(contentLogin);
const header = new Headers({'Content-Type': 'application/json'});
return this._http.post(Config.URL_SITE + 'auth/login', body, {headers: header})
.map(response => response.json())
.catch(error => Observable.throw(error.json()));
}
signin() {
this.token = localStorage['token'];
this.userId = localStorage['userId'];
const body = JSON.stringify({token: this.token, userId: this.userId});
const header = new Headers({'Content-Type': 'application/json'});
return this._http.post(Config.URL_SITE + 'auth/signin', body, {headers: header})
.map(response => response.json().message) //true or false
.catch(error => Observable.throw(error.json()));
}
}
auth.guard.ts
import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Rx';
import {LoginService} from './login/login.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private loginService: LoginService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.loginService.signin()
}
}
这有点奇怪 canActivate
但它确实等待可观察对象完成,而不仅仅是等待可观察对象的事件。
您可以使用
解决方法
return this.loginService.signin().first();
不要忘记导入 take
运算符。
我正在尝试使 canActivate
函数 returns true
或 false
.
布尔值将来自文件 login.service.ts
中的函数 signin()
在 response.json().message
中出现 true
或 false
但当我在文件 auth.guard.ts
.[= 中的 canActivate
函数中使用 this.loginService.logado()
时没有出现值24=]
有人知道如何解决这个问题吗?
login.service.ts
import {Injectable} from "@angular/core";
import {Http, Headers} from "@angular/http";
import {Observable} from "rxjs/Observable";
import 'rxjs/Rx';
import {Config} from '../config';
@Injectable()
export class LoginService{
token: string;
userId: string;
constructor (private _http: Http) {}
login(contentLogin){
const body = JSON.stringify(contentLogin);
const header = new Headers({'Content-Type': 'application/json'});
return this._http.post(Config.URL_SITE + 'auth/login', body, {headers: header})
.map(response => response.json())
.catch(error => Observable.throw(error.json()));
}
signin() {
this.token = localStorage['token'];
this.userId = localStorage['userId'];
const body = JSON.stringify({token: this.token, userId: this.userId});
const header = new Headers({'Content-Type': 'application/json'});
return this._http.post(Config.URL_SITE + 'auth/signin', body, {headers: header})
.map(response => response.json().message) //true or false
.catch(error => Observable.throw(error.json()));
}
}
auth.guard.ts
import {Injectable} from '@angular/core';
import {CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Rx';
import {LoginService} from './login/login.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private loginService: LoginService) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.loginService.signin()
}
}
这有点奇怪 canActivate
但它确实等待可观察对象完成,而不仅仅是等待可观察对象的事件。
您可以使用
解决方法return this.loginService.signin().first();
不要忘记导入 take
运算符。