typescript Angular2 FileSaver.js - 找不到模块“文件保护程序”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39670360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular2 FileSaver.js - Cannot find module 'file-saver'
提问by gamelover42
I am working on moving an angular 1.x site to a new angular 2.0 site I created the site using angular-cli 1.0.0-beta.15.
我正在将一个 angular 1.x 站点移动到一个新的 angular 2.0 站点,我使用 angular-cli 1.0.0-beta.15 创建了该站点。
I have a button to export some data to a CSV file. When the page first loads I get an error message "Cannot find module 'file-saver'", but when I click the button everything works perfectly.
我有一个按钮可以将一些数据导出到 CSV 文件。当页面第一次加载时,我收到一条错误消息“找不到模块‘文件保护程序’”,但是当我单击按钮时,一切正常。
I have the FileSaver.js component installed:
我安装了 FileSaver.js 组件:
package.json
包.json
...
"dependencies": {
...
"@types/filesaver": "0.0.30",
"file-saver": "^1.3.2"
...
}
...
in my export.service.ts:
在我的 export.service.ts 中:
import { saveAs } from 'file-saver';
...
let file = new Blob(['hello world'], { type: 'text/csv;charset=utf-8' });
saveAs(file, 'helloworld.csv');
...
Does anyone know how to resolve this error?
有谁知道如何解决这个错误?
采纳答案by Gregor Biswanger
Install the new TypeScript 2 version.. that should work..
安装新的 TypeScript 2 版本.. 应该可以工作..
回答by Andre Passos
just use this import statment
只需使用此导入语句
import * as fileSaver from 'file-saver';
回答by YairTawil
On angular-cli you should:
在 angular-cli 上你应该:
1) install the library to your node_modules:
1) 将库安装到您的 node_modules:
npm i -S file-saver
2) add reference of js file on 'scripts' in angular-cli.json file:
2) 在 angular-cli.json 文件中的“脚本”上添加 js 文件的引用:
"scripts": ["../node_modules/file-saver/FileSaver.js"]
3) on your typings.d.ts file :
3)在你的typings.d.ts文件上:
declare function saveAs();
after that you can use saveAs() everywhere you need example:
之后,您可以在任何需要的地方使用 saveAs() 示例:
export class MyClass{
constructor(){
saveAs(blablabla...)
}
}
Good Luck!
祝你好运!
回答by Parinda Rajapaksha
Install the Library as follows,
安装库如下,
npm install file-saver --save
This will solve your problem.
这将解决您的问题。