加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

typescript – 将模块导入为类(dojo)

发布时间:2020-12-16 21:18:11 所属栏目:百科 来源:网络整理
导读:背景 目前在我工作的地方,我们使用dojo.requires导入我们在每个类中使用的所有需要??的类.但是,在Dojo 2.0中,他们正在摆脱dojo.require并转到amd require style(http://livedocs.dojotoolkit.org/releasenotes/migration-2.0): require(["dijit/form/Button
背景

目前在我工作的地方,我们使用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....
}

有没有办法可以实现我们想做的事情?

谢谢

解决方法

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

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

你可以使用它加载它

import forms = module("dijit.forms");

var button = new forms.Button();

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读