手动激活aurelia canDeactivate()生命周期挂钩

手动激活aurelia canDeactivate()生命周期挂钩,aurelia,Aurelia,我的应用程序中有两种类型的路由,到aurelia应用程序本身的路由和一些外部路由。我正在使用href属性指定外部路由。在下面的例子中,任务就是这样一个外部路径 { "name": "dashboard", "route": "dashboard", "moduleId": "../dashboard/dashboard", "settings": { "icon": "chart-bar", "helpPageId": "_3000

我的应用程序中有两种类型的路由,到aurelia应用程序本身的路由和一些外部路由。我正在使用
href
属性指定外部路由。在下面的例子中,
任务
就是这样一个外部路径

{
    "name": "dashboard",
    "route": "dashboard",
    "moduleId": "../dashboard/dashboard",
    "settings": {
        "icon": "chart-bar",
        "helpPageId": "_30000"
    },
    "title": "routes:root.dashboard",
    "nav": 25
},
{
    "name": "tasks",
    "settings": {
        "icon": "check-square",
        "helpPageId": "_40000"
    },
    "title": "routes:root.tasks",
    "nav": 30,
    "href": "Task"
}
当用户对aurelia路由进行了一些更改时,我将使用
canDeactivate
hook向用户显示一个自定义对话框,请求放弃更改或保存更改

canDeactivate(): Promise<boolean> {
    //helper method having dialog box logic
    return this.helperMethod.canDiscardChanges(this.modelTracker.isChanged(this.model), this.deactivateSaveCallback.bind(this));
}

这将显示预期的对话框,但不等待用户执行任何操作,与上一步相同,只是在显示对话框后转到指定的路径。任何帮助都是徒劳的。

想想看,你有没有考虑过?也许通过某个步骤,你可以在那里捕捉到正确的事件?
async activate(params: any): Promise<void> {
        // some logic
    window.addEventListener("beforeunload", this.onDomWindowBeforenload);
}

onDomWindowBeforeunload: (event: BeforeUnloadEvent) => void = event => {
    this.canDeactivate();
}

async deactivate(): Promise<void> {
    window.removeEventListener("beforeunload", this.onDomWindowBeforeunload);
}