在 Angular2 中安装 FileSaver 时出错

Error installing FileSaver in Angular2

我正在尝试使用 fileSaver.js https://github.com/eligrey/FileSaver.js 但我遇到了麻烦。

我运行

npm install file-saver --save
npm install @types/file-saver --save-dev

所以在 package.json 我有:

"dependencies": {
    "@angular/animations": "4.1.0",
    "@angular/common": "4.0.0",
    "@angular/compiler": "4.0.0",
    "@angular/core": "4.0.0",
    "@angular/forms": "4.0.0",
    "@angular/http": "4.0.0",
    "@angular/platform-browser": "4.0.0",
    "@angular/platform-browser-dynamic": "4.0.0",
    "@angular/router": "4.0.0",
    "@ngx-translate/core": "6.0.1",
    "@ngx-translate/http-loader": "0.0.3",
    "angular2-busy": "2.0.4",
    "angular2-modal": "2.0.3",
    "core-js": "2.4.1",
    "file-saver": "1.3.3",
    "font-awesome": "4.7.0",
    "intl": "1.2.5",
    "mdn-polyfills": "2.1.1",
    "mydatepicker": "1.8.0",
    "ng2-bootstrap": "1.4.2",
    "ng2-bs3-modal": "0.10.4",
    "primeng": "4.1.0",
    "rxjs": "5.1.0",
    "zone.js": "0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.0.0",
    "@angular/compiler-cli": "4.0.0",
    "@types/file-saver": "0.0.1",
    "@types/jasmine": "2.5.38",
    "@types/node": "6.0.60",
    "codelyzer": "2.0.0",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "3.2.0",
    "karma": "1.4.1",
    "karma-chrome-launcher": "2.0.0",
    "karma-cli": "1.0.1",
    "karma-coverage-istanbul-reporter": "0.2.0",
    "karma-jasmine": "1.1.0",
    "karma-jasmine-html-reporter": "0.2.2",
    "protractor": "5.1.0",
    "ts-node": "2.0.0",
    "tslint": "4.5.0",
    "typescript": "2.2.0"
  }

在app.module中我插入:

import { FileSaver } from 'angular-file-saver';
@NgModule({
  imports: [
    ...
    FileSaver
  ],
...

我有这个错误:

ERROR in FileSaver is not a NgModule

是否安装正确?哪种导入方式正确?

FileSaver 不需要在 app.module 中导入。它只需要在组件中导入:

import * as FileSaver from 'file-saver';

并使用它:

let blob = new Blob([fileText], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "title.txt");