【问题标题】:How to call function of class viewmodel inside it如何在其中调用类viewmodel的函数
【发布时间】:2021-03-24 21:49:39
【问题描述】:

我的导出类出现错误,"AlertMessageIsRead is not a function"

这是我的代码:

class MainViewModel {
    ClickAlertMessage() {
        var currentRecord = this;
        if (currentRecord != null) {
            this.AlertMessageIsRead(currentRecord);
        }
    }

    AlertMessageIsRead(record) {

        var pagename = '';

        AlertMessageService.Factory().IsReadAlertMessage(record.RecordId).then(response => {
            if (response.Result != null) {
                if (this.EntityLookupName == 'ProductStockLimitReached') {
                    pagename = '#product?Id' + '=' + this.RecordId;
                }
                else if (this.EntityLookupName == 'DRDueDateReached') {
                    pagename = '#delivery-receipt?Id' + '=' + this.RecordId;
                }
                this.RedirectPage(pagename);
            }
        });
    }
}

如何拨打我的AlertMessageIsRead? 我的其他函数,我像this.myfunction 一样调用,但在这里,我的this 有一个值。

【问题讨论】:

  • 你不能只使用MainViewModel.AlertMessageIsRead(x) 吗?如果在应用绑定之前没有分配给 var,例如 var vm = new MainViewModel(); ko.applyBindigs(vm),那么您可以使用 vm.AlertMessageIsRead(x) 访问

标签: javascript knockout.js


【解决方案1】:

我们需要看到实际的 HTML 100% 肯定答案,但我觉得这是因为 this 不是您认为的上下文。
而不是像这样调用你的函数:
data-bind="click: ClickAlertMessage"
试试这样:
data-bind="click: () => ClickAlertMessage()"

箭头函数总是传递父上下文。

附:尝试查看上下文console.log(this)

【讨论】:

    猜你喜欢
    • 2021-08-06
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多