0
我希望能夠在Angular組件中創建和下載文件。爲了做到這一點,我使用FileSaver.js。儘管我已經在我的組件進口,我得到這個錯誤控制檯:無法讀取未定義的屬性'saveAs'
ERROR TypeError: Cannot read property 'saveAs' of undefined
這是我的組件:
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { FileSaver } from 'file-saver';
@Component({
moduleId: module.id,
templateUrl: 'home.component.html',
styleUrls: ['home.component.css']
})
export class HomeComponent implements OnInit {
constructor(private http: HttpClient) {
}
nodes: any;
ngOnInit(): void {
// Make the HTTP request:
this.http.get('assets/file.json').subscribe(data => {
// Read the result field from the JSON response.
this.nodes = data;
this.saveFile();
});
}
saveFile=function(){
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "hello world.txt");
}
}
你的意思是,我可以像這樣使用fs變量:'this.fs.saveAs(...)'? – eli
是的。就像你用http – sheplu
所做的那樣,我試過了,現在我又遇到了另外一個錯誤:'無法解析HomeComponent的所有參數:([object Object],?)。' – eli