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

服务

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).subscribe----
//Here how can i Get name from service
  }

最佳答案

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

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);
   })
}

09-20 13:59