将模块导入为类(dojo)

时间:2012-11-23 23:29:52

标签: typescript

背景

目前在我工作的地方,我们使用dojo.requires导入我们在每个类中使用的所有需要​​的类。但是,在Dojo 2.0中,他们正在摆脱dojo.require并转到amd require style(http://livedocs.dojotoolkit.org/releasenotes/migration-2.0):

require(["dijit/form/Button", "dojox/layout/ContentPane", ...], 
   function(Button, ContentPane, ...){
    // CODE HERE
});

我们目前在他们自己的.d.ts文件中定义了如下所示的dojo / dijit类:

module dijit.form{
    export class Button extends dijit.form._FormWidget {
        showLabel : bool;
        _onClick (e:any) : any;
        _onButtonClick (e:any) : any;
        _setShowLabelAttr (val:any) : any;
        _clicked (e:any) : any;
        setLabel (content:String) : any;
        _setLabelAttr (content:String) : any;
        _setIconClassAttr (val:String) : any;
    }
}

这允许我们扩展这些类,如下所示:

class CustomButton extends dijit.form.Button {}

问题

我们希望能够让typescript生成Dojo 2.0(amd)样式需要,并执行以下操作:

import Button = module("dijit/form/Button")
class CustomButton extends Button {}

我们希望编译成以下内容:

define(["require", "exports", "dijit/form/Button"], function(require, exports, Button)    
{
    ///....Generated class    
})

但是,这不起作用,因为import仅适用于模块而不适用于类。我们收到如下错误:

The name '"dijit/form/Button"' does not exist in the current scope
A module cannot be aliased to a non-module type

我们还试图定义dijit类,如:

declare module "dijit/form" {
    export class Button....
}

我们有办法实现我们想做的事吗?

由于

1 个答案:

答案 0 :(得分:1)

在AMD模块中,模块等同于文件,因此如果您有一个名为:

的文件

dijit.forms.ts 甚至 dijit.forms.d.ts

您可以使用

加载它
import forms = module("dijit.forms");

var button = new forms.Button();

在AMD应用程序的定义文件中,您不需要声明模块,因为该文件是模块。

相关问题