[英]How do I include a javascript library with angular 2?
How do I use a javascript library with angular 2? 如何使用角度为2的javascript库? I always get an error saying it's not a module.
我总是得到一个错误,说它不是一个模块。
If I add a script tag to my index.html file pointing to the javascript file, then add it under paths under System.config, then import it, I get an error stating that it's not a module. 如果我将index标签添加到指向javascript文件的index.html文件中,然后将其添加到System.config下的路径下,然后导入它,我会收到一条错误消息,指出它不是模块。
I've already tried this and I've also tried this . 我已经试过了 ,我也试过这个 。 Neither of them work despite the fact they seem to like to point out that it's easy, and that you can use any of hundreds of libraries.
尽管他们似乎想指出它很简单,并且你可以使用数百个库中的任何一个,但它们都没有工作。
Here is a javascript library: sheetjs 这是一个javascript库: sheetjs
How do I make this work in angular 2? 如何在角度2中完成这项工作?
index.html 的index.html
<script src="../node_modules/xlsx/xlsx.js" type="text/javascript"></script>
system-config.ts 系统config.ts
System.config({
...
path: {
xlsx: '../node_modules/xlsx/xlsx.js'
}
});
something.ts something.ts
import * as xlsx from 'xlsx';
Error message: 错误信息:
Error: Typescript found the following errors:
C:/___/tmp/broccoli_type_script_compiler-input_base_path-o4TZ9PSC.tmp/0/src/app/something/something.component.ts (9, 23): Cannot find module 'xlsx'.
my systemjs.config.js 我的systemjs.config.js
(function(global) {
// map tells the System loader where to look for things
var map = {
'src': 'src', // 'dist',
'bin': 'bin',
'@angular': '/node_modules/@angular',
'angular2-in-memory-web-api': '/node_modules/angular2-in-memory-web-api',
'rxjs': '/node_modules/rxjs'
};
// packages tells the System loader how to load when no filename and/or no extension
var packages = {
'src': { defaultExtension: 'js' },
'bin': { defaultExtension: 'js' },
'rxjs': { defaultExtension: 'js' },
'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },
};
var ngPackageNames = [
'common',
'compiler',
'core',
'http',
'platform-browser',
'platform-browser-dynamic',
'router',
'router-deprecated',
'upgrade',
];
// Individual files (~300 requests):
function packIndex(pkgName) {
packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
}
// Bundled (~40 requests):
function packUmd(pkgName) {
packages['@angular/'+pkgName] = { main: pkgName + '.umd.js', defaultExtension: 'js' };
};
// Most environments should use UMD; some (Karma) need the individual index files
var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
// Add package entries for angular packages
ngPackageNames.forEach(setPackageConfig);
var config = {
map: map,
packages: packages
}
System.config(config);
})(this);
include it in the map variable in this. 将它包含在map中的map变量中。
add 'xlsx': { defaultExtension: 'js' }, to packages variable. 将'xlsx':{defaultExtension:'js'}添加到包变量中。 then in your index.html add after the
然后在你的index.html之后添加
Maybe this helps: 也许这有助于:
I noticed, that you are trying to implement the sheetjs/xlsx library. 我注意到,您正在尝试实现sheetjs / xlsx库。 There is a working XLSX library for typescript available here , which works with angular2!
有一个工作XLSX库可用打字稿这里 ,与angular2作品!
If you are looking for a working demo, have a look here . 如果您正在寻找工作演示,请查看此处 。
You dont included your index.html content, but I assume that the <script src="../node_modules/xlsx/xlsx.js" type="text/javascript"></script>
tag is imported before your base component . 您没有包含index.html内容,但我假设在您的基本组件之前导入了
<script src="../node_modules/xlsx/xlsx.js" type="text/javascript"></script>
标记。 If that is true then try to import the script after the base component and on typescript use: declare var xlsx:any;
如果确实如此,那么尝试在基本组件之后导入脚本,并在typescript上使用:
declare var xlsx:any;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.