angular 2 rc1如何导入外部js库

时间:2016-05-26 05:27:33

标签: typescript angular jointjs

我正在尝试将Joint.js与Angular 2一起使用。这就是我所做的。

的index.html

<!-- JoinJS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jointjs/0.9.7/joint.css" />
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.1/backbone-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jointjs/0.9.7/joint.js"></script>  

config.js

  var  map = {
    'app':                        'app', // 'dist',
    'rxjs':                       'https://npmcdn.com/rxjs@5.0.0-beta.6',
    'angular2-in-memory-web-api': 'https://npmcdn.com/angular2-in-memory-web-api', // get latest
    'joint':                      'https://cdnjs.cloudflare.com/ajax/libs/jointjs/0.9.7/joint.js'
  };

  //packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    'app':                        { main: 'main.ts',  defaultExtension: 'ts' },
    'rxjs':                       { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { defaultExtension: 'js' },
    'joint': { defaultExtension: 'js' },
  };

service.ts

//declare var joint: any;
import * as joint from 'joint';

我收到错误&#34; [ts]无法找到模块联合&#34;。

这里缺少什么?

2 个答案:

答案 0 :(得分:3)

您需要包含打字声明文件,以便ts编译器能够理解。全局安装打字( npm install -g typings ),然后安装jointjs typings( typings install dt~jointjs --save --global )文件。 如需参考,请参阅thisthis

答案 1 :(得分:0)

  

我收到错误&#34; [ts]无法找到模块联合&#34;。

FWIW生成的javascript仍然可以正常more

快速修复

您需要在foo.d.ts文件中声明模块:

declare module "join" {
var foo:any;
export = foo;
}

https://basarat.gitbooks.io/typescript/content/docs/types/ambient/d.ts.html