我想将数据表导出到JSON文件我试过这些代码行
import { ErrorHandler } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Component, Input } from '@angular/core';
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
// tslint:disable-next-line:import-blacklist
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class CarService {
handleError: any;
constructor(private http: HttpClient) {
}
getOrderSummary(): Observable<any> {
// get users from api
return this.http.get('assets/ordersummary.json')// , options)
.map((response: Response) => {
console.log('mock data' + response.json());
return response.json();
}
)
.catch(this.handleError);
}
}
我把这些放在appcomponent.ts
中exportToJSON() {
this.carService.getOrderSummary();
}
和appcomponent.html中的这一行
<button (click)="exportToJSON()" class="btn btn-primary">Export to
JSON</button>
但它说失败 - 下载文件后没有文件,原因是什么?
谢谢:))
答案 0 :(得分:0)
使用新的HttpClient
课程时,您无需致电map
getOrderSummary(): Observable<any> {
// get users from api
return this.http.get('/assets/ordersummary.json')// , options)
.catch(this.handleError);
}
您还应该在网址的开头添加斜杠,以便您可以从网站中的任何深层网址调用
/assets/ordersummary.json
最后,您应该定义handleError
答案 1 :(得分:0)
试试这个:
服务:
getOrderSummary(): Observable<any> {
// get users from api
return this.http.get('assets/ordersummary.json')// , options);
}
成分:
exportToJSON() {
this.carService.getOrderSummary()
.subscribe((a) => {console.log('result', a);}, (error) => {/*handle error*/});
}
并确保您的网址有效。