简体   繁体   English

设置属性以触发Typescript类中的方法

[英]Set property to trigger method inside typescript class

I currently trying to setup a property inside my class so that when it is called, it will trigger a method inside that same class to execute. 我目前正在尝试在类中设置属性,以便在调用该属性时会触发该类中的方法来执行。 I seem to be getting a lot of errors relating to ... is not a function . 我似乎会遇到很多与... is not a function有关的错误, ... is not a function

So I Have tried to implement this a different way like below. 因此,我尝试过采用以下类似的方法来实现此目的。

So I have setup my class with a lot of properties and a constructor, I have tried to setup a property html that will trigger the method getHTML() to be fired: 因此,我为类设置了很多属性和构造函数,并尝试设置属性html,它将触发方法getHTML()被触发:

export class DocumentTemplateHistoryDto {
Id: string;
Agent: AgentDto;
AgentId: string;
CreatedDate: Date;
Draft: boolean;
EndDate: Date;
HeaderAndFooter: HeaderAndFooterDto;
HeaderAndFooterId: string;
Live: boolean;
StartDate: Date;
DocumentTemplate: DocumentTemplateDto;
DocumentTemplateId: string;
VersionNumber: number;
Pages: PageDto[];
_MergedHTML: string;
html: () => void;

constructor(_Agent: AgentDto, _AgentId: string, _CreatedDate: Date, _Draft: boolean, _EndDate: Date, _HeaderAndFooter: HeaderAndFooterDto, _HeaderAndFooterId: string,
    _Live: boolean, _StartDate: Date, _DocumentTemplate: DocumentTemplateDto, _DocumentTemplateId: string, _VersionNumber: number, _Pages: PageDto[], _Id?: string) {

    this.Agent = _Agent;
    this.AgentId = _AgentId;
    this.CreatedDate = _CreatedDate;
    this.Draft = _Draft;
    this.EndDate = _EndDate;
    this.HeaderAndFooter = _HeaderAndFooter;
    this.HeaderAndFooterId = _HeaderAndFooterId;
    this.Live = _Live;
    this.StartDate = _StartDate;
    this.DocumentTemplate = _DocumentTemplate;
    this.DocumentTemplateId = _DocumentTemplateId;
    this.VersionNumber = _VersionNumber;
    this.Pages = _Pages;
    this.Id = _Id;


}

getHTML() {
    this.Pages.forEach((item) => {
        this._MergedHTML = this._MergedHTML + item.HTML;
    });
}
}

I cannot seem to get the syntax right for this functionality. 我似乎无法正确使用此功能的语法。 Could someone help me out please. 有人可以帮我吗。

There are a few ways to do this, but the ones that immediately come to mind for me are a getter or proxy. 有几种方法可以做到这一点,但立即想到的是吸气剂或代理。

Getters and setters are probably the easiest way to implement what you want, as shown below: getter和setter可能是实现所需内容的最简单方法,如下所示:

export class DocumentTemplateHistoryDto {

    ...

    public get html(){
        this.Pages.forEach((item) => {
            this._MergedHTML = this._MergedHTML + item.HTML;
        });
    }
}

Then call it: 然后调用它:

new DocumentTemplateHistoryDto().html;

Alertnatively, you could set up a proxy: 明智地,您可以设置一个代理:

export class DocumentTemplateHistoryDto {

...

public htmlProxy = new Proxy ({
    get: (obj, prop) => {
        if(prop === 'html')
            this.Pages.forEach((item) => {
                this._MergedHTML = this._MergedHTML + item.HTML;
            });
      return true;
    }
}); 

Then call it: 然后调用它:

new DocumentTemplateHistoryDto().htmlProxy.html;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM