在打字稿中导出类和函数

时间:2019-12-12 08:59:49

标签: angular typescript

在我的angularJS中,我有一个单独的JS文件,其中的原型函数声明如下:

function lastConversation(){
 this.item1="1"
 this.message="test"
......

}
lastConversation.prototype.setupfromMessageThread(data) {
  this.currentBox = data.currentBox ||  null;
  data.internalId = data.internalId ||  null;
  this.addParticipants(data.participants);
}

lastConversation.prototype.addParticipants = function (participants) {
}

我想在angular 6中创建类似的东西并创建类

export class conversation {

constructor(json?: any) {
    if (!json) return;
   this.item=json.internalId || 0;
  }

 setupfromMessageThread(conversation) {
    this.internalId = conversation.internalId || null; }}

但是,当我在ts文件中使用

lastconveration = new dataModel.Conversation()
lastconveration.setupfromMessageThread(conversation) 

它说setupfromMessageThread不起作用。如果我想使用相同的angularJs函数,是否需要在每个函数之前使用export,或者如果我要使用angular6,我要缺少什么。

2 个答案:

答案 0 :(得分:0)

您为什么这样做:

lastconveration = new dataModel.Conversation(); 
lastconveration.setupfromMessageThread(conversation); 
  

只需使用this

调用您的方法
this.setupfromMessageThread(conversation) 

答案 1 :(得分:0)

要将lastConversation函数表示为类:

class LastConverstion { 
    constructor(){
        this.item1="1"
        this.message="test"
         //......
    }

    setupfromMessageThread(data) {
      this.currentBox = data.currentBox ||  null;
      data.internalId = data.internalId ||  null;
      this.addParticipants(data.participants);
    }

    addParticipants (participants) {
    }
}

常见的做法是将类的名称首字母大写。

用法

let instance = new LastConversation();
相关问题