我如何在返回字符串中获得Observable值

时间:2019-06-13 06:27:06

标签: angular typescript

我是新来的人,请帮助我如何从服务中获取组件中的返回值

Service.ts

name:string = 'John';
 GetSecureTokene(_hero:Users){    
    debugger;
    if(_hero.Email=='abc' && _hero.PassWord==123){
      return this.name;     
    }

Component.ts

submitForm(vale:any){
    debugger;
   this._AuthServiceService.GetSecureTokene(vale).subscxribe----
//Here how can i Get name from service 
  }

1 个答案:

答案 0 :(得分:1)

将代码从您的服务更改为此

import { of, Observable } from "rxjs";

GetSecureTokene(_hero:Users): Observable<string> {    
 if(_hero.Email=='abc' && _hero.PassWord==123){
    return of(this.name);     
 }
}

在您的组件中

submitForm(vale:any) {
   this._AuthServiceService.GetSecureTokene(vale).subscribe(res => {
     console.log(res);
   })
}