我开发了示例POC来保存后端API提供的excel文件。在UI中,我将Angular 2与fileaver.js一起使用。
现在,当我尝试调用文件保护程序的"另存为"功能时,chrome给出了以下错误
类型错误:WEBPACK_IMPORTED_MODULE_2_file_saver.saveAs 不是函数
这是我的包.json
"文件保护程序": "^1.3.3",
这是我的 angular-cli.json
"脚本":["../node_modules/file-saver/FileSaver.js"],
这是我的 Angular2 服务组件
exportFile(){
this._http.get(this.url,{responseType : ResponseContentType.Blob})
.map( (response) => {
let blob = response.blob();
console.log('Received excel data....');
return{
data : new Blob([blob],{type : 'application/vnd.ms-excel'}),
filename : 'test.xls'
}
})
.subscribe(
(res)=>{
console.log('Exporting excel file');
FileSaver.saveAs(res.data,res.filename);
}
)
}
在顶部,我导入了文件保护程序,如下所示
从"文件保护程序"导入 * 作为文件保护程序;
整个项目位于 github 上
角度2导出演示
我认为如果 angular-cli.json 在您的包中,则不应该在 angular-cli.json 中进行导入.json尝试从 angular-cli 中删除行"../node_modules/file-saver/FileSaver.js"
,然后从node_modules中删除文件保护程序目录,然后执行npm install
或更好的yarn install
。