2017-10-10 2 views
0

각도 요소로 파일을 만들고 다운로드 할 수있게하려고합니다. 그렇게하기 위해 FileSaver.js를 사용하고 있습니다. 내 구성 요소를 가져온에도 불구하고, 나는 콘솔에서이 오류를 얻을 :정의되지 않은 'saveAs'속성을 읽을 수 없습니다.

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"); 


} 


} 

답변

0

는 생성자와 FileSaver를 선언 사용 하다며 수 있기 : ERROR TypeError: Cannot read property 'saveAs' of undefined

내 구성 요소입니다

constructor(private http: HttpClient, private fs : FileSaver) { 

} 

그리고 나서 fs 변수를 사용하십시오.

+0

당신은 다음과 같은 fs 변수를 사용할 수 있습니다 :'this.fs.saveAs (...)'? – eli

+0

예. 당신이 http – sheplu

+0

처럼했는데, 이제는 다른 오류가 발생했습니다 :'HomeComponent에 대한 모든 매개 변수를 해결할 수 없습니다 : ([object Object],?). – eli

관련 문제