TypeScript 加载AMD模块

了解过nodejs的话应该熟悉下面的语法


math.js

exports.add = function(n1, n2){
	return n1 + n2;
}

server.js

var math = require('./math.js');
math.add(1,2);

这样就可以在js文件中导入js文件,但在web开发中我们通常要在html页面中按照顺序导入所有js文件, 在TypeScript中也提供了类似的方法。

Compoment.ts

export class Compoment {
    private element: Element;
    private id: string;
    constructor (public config: Object) {
        
    }
    initialize() { 
    
    }
    initCompoment() { 
        
    }
}

panel.ts

import mod = module("Compoment");
class Panel extends mod.Compoment{
    constructor (config) { 
        super(config);
        console.log(this.config);
    }
}

接下来编译时使用 tsc panel.ts 是得不到想要的效果的,需要使用AMD模块来编译 tsc --module amd panel.ts 并在html页面中加入require.js并指定入口。

<script data-main="panel" type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.1/require.min.js"></script>

接下来就可以在浏览器上测试了


你可能感兴趣的:(TypeScript 加载AMD模块)