如何在角度2中使用SheetJS(js-xlsx) [英] how to use SheetJS (js-xlsx) in angular 2
问题描述
我正在学习angular2,我想在我的项目中使用 js-xlsx 库.
I'm learning angular2 and i wanted to use js-xlsx library in my project.
我安装了xlsx npm install xlsx
和jszip npm install jszip
并将它们添加到我的index.html
I installed xlsx npm install xlsx
and jszip npm install jszip
and added them in my index.html
<script src="node_modules/xlsx/dist/xlsx.core.min.js"></script>
<script src="node_modules/jszip/dist/jszip.min.js"></script>
并添加了打字稿定义tsd install xlsx
我正在使用webpack
and I'm using webpack
但是当我在
import * as xlsx from 'xlsx';
但是我得到了错误
module build failed: error: cannot resolve module 'xlsx'
推荐答案
更简单的方法是根本不使用类型输入:
An easier method would be not using typings at all:
-
像以前一样将xlsx.core.min.js添加到index.html文件中. (我正在使用angular-cli,因此我的js文件被复制到dist/vendor目录中)
Add xlsx.core.min.js to your index.html file as you did. (I'm using angular-cli so my js files get copied to the dist/vendor directory)
<script src="vendor/xlsx/dist/xlsx.core.min.js"></script>
在您的模块中,不要使用import,而是在import块下声明XLSX.
In your module, do not use import but declare XLSX under your imports block.
declare var XLSX: any;
如果您使用的是angular-cli,则需要将xlsx添加到angular-cli-build.js文件中.
If you're using angular-cli, you will need to add xlsx to your angular-cli-build.js file.
module.exports = function(defaults) {
return new Angular2App(defaults, {
vendorNpmFiles: [
'systemjs/dist/system-polyfills.js',
'systemjs/dist/system.src.js',
'zone.js/dist/**/*.+(js|js.map)',
'es6-shim/es6-shim.js',
'reflect-metadata/**/*.+(ts|js|js.map)',
'rxjs/**/*.+(js|js.map)',
'@angular/**/*.+(js|js.map)',
'moment/moment.js',
....
'xlsx/**/*.+(js|js.map)'
]})
};
这篇关于如何在角度2中使用SheetJS(js-xlsx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!