【问题标题】:Ionic v5/Angular 8: HttpClient.get().subscribe() returns TypeError: Cannot read property 'handle' of undefinedIonic v5/Angular 8:HttpClient.get().subscribe() 返回 TypeError:无法读取未定义的属性“句柄”
【发布时间】:2019-11-20 21:43:05
【问题描述】:

HttpClient.get('...').subscribe((data:any) => { console.log(data) })) 导致错误,我无法找到问题所在。请问,你知道出了什么问题吗?我是 Angular 初学者。

我得到了 this.httpObserve 的可能有效的 Observe 对象 订阅观察对象会导致此错误。

ERROR TypeError: Cannot read property 'handle' of undefined (core.js:9110)
    at MergeMapSubscriber.project (http.js:1246)
    at MergeMapSubscriber._tryNext (mergeMap.js:46)
    at MergeMapSubscriber._next (mergeMap.js:36)
    at MergeMapSubscriber.next (Subscriber.js:49)
    at Observable._subscribe (subscribeToArray.js:3)
    at Observable._trySubscribe (Observable.js:42)
    at Observable.subscribe (Observable.js:28)
    at MergeMapOperator.call (mergeMap.js:21)
    at Observable.subscribe (Observable.js:23)
    at FilterOperator.call (filter.js:13)

----------- apicall.service.ts ----------------
import { Injectable } from '@angular/core';
import { HttpClient, HttpHandler, HttpClientModule } from '@angular/common/http';
import { Observable } from "rxjs";


@Injectable({
  providedIn: 'root'
})
export class ApicallService {
  httpObserve;
  httpHandler : HttpHandler;

  constructor() { }

  http = new HttpClient(this.httpHandler);

  public getData(val) : any {
    // test url
    this.httpObserve = this.http.get('https://www.seznam.cz');
    return this.httpObserve;
  }
}

----------- myapi.component.ts -------------------
import { Component, OnInit } from '@angular/core';
import { ApicallService } from '../apicall.service';
import { Observable } from "rxjs";

@Component({
  selector: 'app-myapi',
  templateUrl: './myapi.component.html',
  styleUrls: ['./myapi.component.scss'],
})

export class MyapiComponent implements OnInit {
  result : any;
  httpObserve;

  constructor() {}

  apicall = new ApicallService();

  ngOnInit(){}

  get(val): void {
    this.httpObserve = this.apicall.getData(val);
    this.httpObserve.subscribe((data:any) => { console.log(data) });
  }
}

page.ts

myapi = new MyapiComponent();
this.res = this.myapi.get(val);

【问题讨论】:

    标签: angular ionic-framework httpclient


    【解决方案1】:

    您在 httpHandler 中没有任何值就发起了。

    http = new HttpClient(this.httpHandler); // <-- his.httpHandler is null
    

    如果不需要httpHandler

    constructor(private http: HttpClient) { }
    
    // remove httpHandler : HttpHandler;
    // remove  http = new HttpClient(this.httpHandler);
    

    【讨论】:

    • 是的,因为我找不到应该有什么论据:(请问我应该做些什么改变?
    • 如果不使用HttpHandler,则将httpClient注入到你的构造函数中使用
    • 但是,现在最好的办法是学习Angular Basic
    • 是的,感谢您提供信息。是的,我是 Angular 的新手,我必须掌握基础知识。
    猜你喜欢
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2019-12-07
    • 2019-11-08
    • 2015-03-28
    • 1970-01-01
    相关资源
    最近更新 更多