简体   繁体   English

typescript 私有变量消息

[英]typescript private variables message

In my typescript class I have two private variables.在我的 typescript class 我有两个私有变量。 One variable is goping to store a string that I am returning from an API.一个变量将存储我从 API 返回的字符串。 The other one is a message that I will be displaying to the user.另一个是我将向用户显示的消息。 Once the API is completed and will use the return and build the full message.一旦 API 完成,将使用返回并构建完整消息。 In the response I am setting this.templatIds so it will build the message with the correct ids that.在响应中,我设置this.templatIds ,因此它将使用正确的 id 构建消息。 But in my popup message its missing the section from the string.但是在我的弹出消息中,它缺少字符串中的部分。

export class AgentsGridComponent implements AfterViewInit {
    private templatIds = "";
    private inuseItemMessage = `This Agent is being used by Templates ${this.templatIds} and can't be archived`




this.agentsService.inuseTemplateAgentList(data.item.Id)
                    .then((response) => {
                        console.log(response.Value);
                        this.templatIds = response.Value;
                        this.notificationManager.error(inuseItemMessage);
                    });

The templatIds is not populated at the time that the inuseItemMessage is assigned to - they both get assigned at the same time, at the beginning of the constructor.templatIds时不会填充inuseItemMessage - 它们都在构造函数的开头同时分配。 Make the message into a function instead, so it can construct the template string from the now-populated templatIds on demand.将消息改为function ,以便它可以根据需要从现在填充的templatIds构造模板字符串。

private getInUseMessage = () => `This Agent is being used by Templates ${this.templatIds} and can't be archived`
this.notificationManager.error(this.getInUseMessage());

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

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