首页 文章

Ionic 3 http请求

提问于
浏览
0

每当http请求(使用rxjs)是在那个时间来自真实设备并且接收到服务的引用者是未定义的时候 .

Http请求无法按预期从真实设备工作 .

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';

@Injectable()
export class DbcallService {
  constructor(private _http: Http) {}

  getData() {
    var url = 'https://jsonplaceholder.typicode.com/posts';
    this._http.get(url).subscribe(data => {
      console.log(data);
    });
  }
}

2 回答

  • 0

    你必须改变它,如下所示 .

    您-provider.ts

    import { Injectable } from '@angular/core';
    import { Http } from "@angular/http";
    import { Observable } from 'rxjs/Observable';
    import 'rxjs/add/operator/map';
    
    @Injectable()
    export class DbcallService {
      constructor(private _http: Http) {
      }
    
      getData() {
        var url = 'https://jsonplaceholder.typicode.com/posts';
        this._http.get(url).map(res => res.json());
      }
    }
    

    当你打电话时,你必须这样做 .

    我-page.ts

    this.myProvider.getData().subscribe(
          result => {
           },
          error => { },
          () => { }
        );
    
  • 0

    使用HttpClient代替Angular 6 .

    这有效:

    import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    
    @Injectable()
    export class DbcallService {
      constructor(private _http: HttpClient) {
      }
    
      getData() {
        var url = 'https://jsonplaceholder.typicode.com/posts';
        this._http.get(url).subscribe(data => {
          console.log(data);
        });
      }
    }
    

相关问题