在knockoutjs中结合href, click和data-bind

Combine href, click and data-bind in knockoutjs

本文关键字:click data-bind href knockoutjs 结合      更新时间:2023-09-26

我试图在等待慢速链接时显示某种加载器:

<a href="/api/action/that/takes/some/time" data-bind="click: showLoading">
this.showLoading = function () {
    // Display loader while waiting for the redirect
}

click似乎覆盖了实际的链接。有办法解决这个问题吗?

澄清编辑:我可以这样做,但我更喜欢把url放在href上,只把showLoading位加到那些链接上,这需要一些时间

<a href="#" data-bind="click: showLoading.bind($data, '/api/action/that/takes/some/time'">
this.showLoading = function(link) {
    // Display loader while waiting for the redirect
    window.location.href = link;
};

您只需要从您的click处理程序return true触发浏览器的默认操作:

this.showLoading = function () {
    // Display loader while waiting for the redirect
    return true;
}

请参阅文档:允许默认的点击动作