Durandal路由IF语句

Durandal routing IF statement

我在 shell.js 文件中的一个按钮上有一个点击绑定。点击带你返回

function goBack() {
return router.navigateBack();
}

我有一个特定的路线(reportForm),我不希望它复制这个确切的行为。我正在尝试在返回之前介绍一条确认消息。

    function goBack() {

        if (router.routes[7].moduleId === "viewmodels/reportForm") {
            return router.navigateBack();
        } else {
            return app.showMessage(strings.logoutWarning, strings.logout, [strings.no, strings.yes])
                .then(function (answer) {
                    if (answer === strings.yes) {
                        return router.navigateBack();
                    }
                })
        }
    }

看起来它不知道它当前的路线。我需要找出我当前的位置,看看这是否匹配。它目前正在检查是否存在它总是这样做 returns 相同的语句。如果有 Durandal 经验的人可以帮助我实现上面我想做的事情,我将非常感激。

这是我的 Durandal 路由器映射(我想显示此消息的 reportForm):

function activate() {
    router.map([
        { route: '', title: 'Welcome', moduleId: 'viewmodels/login', nav: true },
        { route: 'home', moduleId: 'viewmodels/home', nav: true },
        { route: 'log', moduleId: 'viewmodels/log', nav: true },
        { route: 'locationSelect', moduleId: 'viewmodels/locationSelect', nav: true },
        { route: 'history', moduleId: 'viewmodels/history', nav: true },
        { route: 'helpText', moduleId: 'viewmodels/helpText', nav: true },
        { route: 'scheduled', moduleId: 'viewmodels/scheduled', nav: true },
        { route: 'reportForm/:formId(/:templateId)', moduleId: 'viewmodels/reportForm', nav: true }
    ]).buildNavigationModel();

    return router.activate();
}

您可以查看 router.activeInstruction().params[0] 值,它为您提供了即将导航到的路线。在您的 canDeactivate(或您处理此问题的任何其他地方)生命周期回调中,您可以将您的逻辑用于向用户显示确认消息,然后进行相应的导航。以下是您可以尝试的内容 -

function goBack() {    
        if (router.activeInstruction().params[0] !== null && typeof(router.activeInstruction().params[0]) !== "undefined" && router.activeInstruction().params[0].indexOf("reportForm") >= 0) {
            return router.navigateBack();
        } else {
            return app.showMessage(strings.logoutWarning, strings.logout, [strings.no, strings.yes])
                .then(function (answer) {
                    if (answer === strings.yes) {
                        return router.navigateBack();
                    }
                })
        }
    }

希望对您有所帮助。