【问题标题】:How to return object from Observable in Angular 7如何从 Angular 7 中的 Observable 返回对象
【发布时间】:2019-06-25 21:37:28
【问题描述】:

我在 Angular 7 和 RxJS@6.3.3 上有一个应用程序。 我试图从我的 Observable 对象中获取价值,但我不知道为什么它没有返回。

有我的示例代码:https://stackblitz.com/edit/angular-w9ftkw

在我的示例 html 文件中,我有:

<hello name="{{ result.property1 }} {{ result.property2 }} {{ result.property3 }} {{ resultIsUndefined }}"></hello>
<p>
  {{err}}
</p>

但是我的结果的属性({{ result.property1 }} {{ result.property2 }} {{ result.property3 }})没有显示出来。

这有什么问题?

我试图返回的res 对象仍然是Observable 的类型。我试图将它投射到MyResponseClasss,但没有效果。

这是我真正的问题。为什么返回的对象仍然是Observable的类型。

关于这部分代码:

if (result) {
  result.subscribe((result: MyResponseClass) => {
    this.resultIsUndefined = false;
    res = result;
    console.log(res);
  })
  return res;
}

我想要一个res,数据类型为MyResponseClass。

【问题讨论】:

  • 你想通过调用一个空 url 并期望它返回响应而不执行你的 catchError 块来实现什么?
  • 空 url 仅在示例代码上。我的应用程序正在发送真实请求。

标签: angular observable angular7 rxjs6


【解决方案1】:

当你的数据来自 Observable 时,你应该考虑使用异步管道 {{ 不管 |异步 }}

更多阅读:

【讨论】:

    【解决方案2】:

    您需要将 json 对象字符串化以在 html 中显示。像这样

    let result = this.http.request("get", url_, options_).pipe(
    map((response_: any) => this.test(response_)),
    catchError((err: any, caught: Observable<any>) => {
    **this.err = JSON.stringify(err);**
    if (caught instanceof Response) {
    try {
       return this.test(caught);
    } catch (e) {
    return of<MyResponseClass | null>(<any>throwError(e));
    }
    } else
      return of<MyResponseClass | null>(<any>throwError(caught));
    })
    );
    

    【讨论】:

    • 我想显示 "{{ result.property1 }} {{ result.property2 }} {{ result.property3 }}" 不是错误
    【解决方案3】:

    您所要做的就是添加 json 管道。

    像这样:

    {{ err | json}}
    

    或者如果你确切地知道你想要使用它的对象内部的什么属性 示例:

    {{err.message}}
    

    【讨论】:

    • 我想显示 "{{ result.property1 }} {{ result.property2 }} {{ result.property3 }}" 不是错误
    【解决方案4】:

    我解决了我的问题。 可以通过两种方式创建 Observable 对象:

    • 使用 Observable
    • 作为异步
    • 使用of同步

    我的问题是我在异步应用程序上创建了 Observable 对象作为同步。 就是这样。

    【讨论】:

      【解决方案5】:

      您正在收到此课程的回复。

      protected test(response: Response): Observable<MyResponseClass>{};
      

      在这个类中你需要调用http服务调用。

      protected test(response: Response): Observable<MyResponseClass>{
      this.http.get(url,options);
      };
      

      然后你需要在你的 ts 类中订阅观察者。

      export class AppComponent  {
      err: string;
      resultIsUndefined: boolean;
      http: HttpClient;
      result: MyResponseClass;
      name: string;
      constructor(@Inject(HttpClient) http: HttpClient) {
      this.http = http;
      this.result = this.get();
      }
      protected test(): Observable<MyResponseClass> { 
      let result = new MyResponseClass();
      result.property1 = "Mr";
      result.property2 = "John";
      result.property3 = "Kowalsky";
      return of<MyResponseClass>(result); 
      }
      get(): MyResponseClass {
      let res = new MyResponseClass();
      let url_ = '';
      let options_: any = {
      method: "get",
      headers: new Headers({
      "Content-Type": "application/json",
      "Accept": "application/json"
      })
      };
      this.test().subscribe(res =>{
      this.name = res.property1 + res.property2 + res.property3;
      console.log(res, this.name);
      },
      error=>{
      this.err = JSON.stringify(error);
      console.log(error);
      })
      this.resultIsUndefined = true;
      return <MyResponseClass>null;
      }
      }
      

      在 app.component.html 中需要是这样的

      <hello [name]="name"></hello>
      <p>
      {{err}}
      </p>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-12
        • 2021-09-24
        • 2020-11-25
        • 2017-03-24
        • 1970-01-01
        • 2021-11-26
        • 1970-01-01
        相关资源
        最近更新 更多