【问题标题】:Can't get data from JSON file无法从 JSON 文件中获取数据
【发布时间】:2017-06-28 18:28:54
【问题描述】:

我无法获取 JSON 本地文件 Angular HTTP 服务。我有这些代码:

import {Injectable} from '@angular/core';
import {Http, Response} from "@angular/http";
import 'rxjs/add/operator/map'


@Injectable()
export class ApiService {

constructor(private http:Http) {
}


private coinsUrl:string = 'app/data/coins.json';

getMines() {
  return this.http.get(this.coinsUrl).subscribe(
    (res:Response)=> {
      const sss = res.json();
      console.log('sss', sss);
    }

  );
}

}

【问题讨论】:

    标签: json angular angular2-http


    【解决方案1】:

    由于您使用的是 angular-cli,因此您在运行时将无法使用该文件位置(使用 ng serve 或生产版本)。将文件放入assets文件夹,然后就可以加载/assets/coins.json了。

    【讨论】:

      猜你喜欢
      • 2019-12-16
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多