微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 使用Sammy.js取消路线,不影响历史记录

我想拦截所有路由更改与Sammy首先检查是否有待处理的操作.我已经使用sammy.before API完成了这一点,我返回false以取消路由.这将使用户保持在“页面”上,但仍会更改浏览器地址栏中的哈希值,并将路由添加到浏览器的历史记录.如果我取消路线,我不希望在地址栏和历史,但我希望地址保持不变.

目前,为了解决这个问题,我可以调用window.history.back(yuk)返回历史上的原始位置或sammy.redirect.这两个都不太理想.

有没有办法使sammy真正取消路由,所以它保持在当前的路线/页面,离开地址栏,并不添加到历史?

如果没有,是否有另一个路由库会这样做?

sammy.before(/.*/,function () {
    // Can cancel the route if this returns false
    var response = routeMediator.canLeave();

if (!isRedirecting && !response.val) {
    isRedirecting = true;
    // Keep hash url the same in address bar
    window.history.back();
    //this.redirect('#/SpecificPrevIoUsPage'); 
}
else {
    isRedirecting = false;
}
return response.val;
});

解决方法

如果有人遇到这种情况,这里是我最后的地方.我决定使用sammy的context.setLocation功能来处理重置路由.
sammy.before(/.*/,function () {
    // Can cancel the route if this returns false
    var
        context = this,response = routeMediator.canLeave();

    if (!isRedirecting && !response.val) {
        isRedirecting = true;
        toastr.warning(response.message); // toastr displays the message
        // Keep hash url the same in address bar
        context.app.setLocation(currentHash);
    }
    else {
        isRedirecting = false;
        currentHash = context.app.getLocation();
    }
    return response.val;
});

原文地址:https://www.jb51.cc/js/153337.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐