【发布时间】:2014-12-04 17:00:11
【问题描述】:
我正在尝试使用 meanjs 实现 filesaver.js。但它不起作用。
是否可以和meanjs一起使用??
我已按照以下步骤操作。
1- 安装文件保护程序后,使用命令通过 bower 上传文件:
bower install file-saver --save
2- 我在我的应用程序布局页面的文件中包含了对 fileSaver.js 文件的引用:config\env\all.js,如下所示:
assets: {
lib: {
css: [
'public/lib/bootstrap/dist/css/bootstrap.css',
'public/lib/bootstrap/dist/css/bootstrap-theme.css'
],
js: [
'public/lib/angular/angular.js',
'public/lib/file-saver/FileSaver.min.js',
'public/lib/file-saver/FileSaver.js',
]
},
3- 然后我在我的 Angular 应用程序中包含 filesaver 作为依赖项,在文件:public\config.js 中,通过在此行的数组末尾添加“fileSaver”:
var applicationModuleVendorDependencies = ['ngResource', 'ngCookies', 'ngAnimate', 'ngTouch', 'ngSanitize', 'ui.router', 'ui.bootstrap', 'ui.utils', 'fileSaver'];
4- 我正在尝试使用我的 meanjs 客户端控制器中的以下代码导出到 XL 表。
$scope.export = function($event) {
$event.preventDefault();
$event.stopPropagation();
var blob = new Blob([document.getElementById('exportable').innerHTM], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
});
saveAs(blob, 'Report.xls');
};
在我看来,我无法使用文件保护程序。它警告说未定义“saveAs”。
谢谢。
【问题讨论】:
-
我会使用纯 JavaScript。使用 MEAN 并不意味着(没有双关语)您必须采用 Angular 方式。我不是 JS 专家,但你不能只添加一个纯 JS 库并假设你可以通过依赖注入“导入”它。我建议您构建一个服务,将所有纯 javascript 放在那里,在“纯”javascript(fileSaver JS 库)中编写代码并返回 fileSaver 执行的 CRUD 操作的结果。然后你只需通过 DI 将你的服务连接到你的控制器,瞧。
标签: javascript angularjs meanjs