在Angular2中安装FileSaver时出错

时间:2017-09-07 13:03:30

标签: angular filesaver.js

我正在尝试使用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

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

1 个答案:

答案 0 :(得分:1)

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