Angular文件保存程序未知提供程序

时间:2019-05-16 11:49:48

标签: javascript angularjs bower

我使用以下命令安装了angular-file-saver

bower install angular-file-saver

然后我在我的bower.json文件中添加了依赖项:

{
  "name": "app",
  "version": "0.0.0",
  "dependencies": {
    "angular": "^1.4.0",
    "bootstrap": "^3.2.0",
    "file-saver.js": "^1.20150507.2"
  },
  "devDependencies": {
    "angular-mocks": "^1.4.0"
  },
  "appPath": "app",
  "moduleName": "app",
  "overrides": {
    "bootstrap": {
      "main": [
        "less/bootstrap.less",
        "dist/css/bootstrap.css",
        "dist/js/bootstrap.js"
      ]
    }
  }
}

file-saver.js中的bower.json是

{
  "name": "file-saver.js",
  "main": "FileSaver.js",
  "version": "1.20150507.2",
  "homepage": "https://github.com/Teleborder/FileSaver.js",
  "authors": [
    "Eli Grey (http://eligrey.com)"
  ],
  "description": "A saveAs() FileSaver implementation",
  "keywords": [
    "File",
    "FileSaver",
    "saveAs"
  ],
  "license": "LICENSE.md",
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components",
    "test",
    "tests",
    "demo",
    "FileSaver.min.js"
  ]
}

然后将其导入控制器中:

controller('MainCtrl', ['$http', 'file-saver.js', function ($http, fs) {  

但是我得到了错误:

Error: "[$injector:unpr] Unknown provider: file-saver.jsProvider <- file-saver.js <- MainCtrl
https://errors.angularjs.org/1.7.8/$injector/unpr?p0=file-saver.jsProvider%20%3C-%20file-saver.js%20%3C-%20MainCtrl"

我尝试了其他名称,例如文件保护程序,FileSaver,FileSaver.js,但没有用。

1 个答案:

答案 0 :(得分:0)

文档说,您需要在模块中导入ngFileSaver模块,然后在组件中注入FileSaver。这是提供的示例:

angular
  .module('fileSaverExample', ['ngFileSaver'])
  .controller('ExampleCtrl', ['FileSaver', 'Blob', ExampleCtrl]);

之后,您可以调用FileSaver.saveAs方法。