【问题标题】:Durandal- Uncaught Error: History has already been activatedDurandal-未捕获的错误:历史记录已被激活
【发布时间】:2014-04-16 06:59:35
【问题描述】:

在我的项目中,我有一个登录屏幕,然后是几个导航页面和注销。一旦我注销并尝试再次登录,我会收到一个错误:未捕获的错误:历史记录已被激活。

我应该如何在每次注销时停用历史记录?

我在所有页面中添加了 deactivate 方法,并将视图视图模型设置为 null。注销时我需要注意哪些事项?任何代码示例都会有所帮助。

在 main.js 我有以下代码:

define(['durandal/system', 'durandal/app', 'durandal/viewLocator', 'plugins/router'],
    function (system, app, viewLocator, router) {

        system.debug(true);

        app.configurePlugins({
            router: true
        });

        app.start().then(function () {

            viewLocator.useConvention();

            app.setRoot('viewmodels/login');

            system.log('Main Module started');
        });


    });

成功登录后,我将应用程序根设置为 shell。 shell.js中的代码如下:

define(['plugins/router', 'durandal/system', 'durandal/app'], function (router, system, app) {
    return {
        title: 'Configuration',
        router: router,
        activate: function () {

            system.log('Configuring routes');

            router.map([
                {
                    route: '', moduleId: 'viewmodels/home', nav: true,
                    title: 'Home'
                },
                {
                    route: 'a1', moduleId: 'viewmodels/a1', nav: true,
                    title: 'A1'
                },
                {
                    route: 'a2', moduleId: 'viewmodels/a2', nav: true,
                    title: 'a2'
                }

            ]).buildNavigationModel();

            system.log('Shell view activating');


            return router.activate();
        },

        logout: function () {
            app.setRoot('viewmodels/login');
        },

        deactivate: function () {

            router.deactivate();
            router = null;
            console.log("shell js- deactivate.. ");
        },


    }
});

在 deactivate 方法中,我将路由器设置为 null。

应该怎么做才能解决这个问题?

谢谢。

【问题讨论】:

  • 只是想知道,为什么需要停用路由器?就个人而言,我从来没有这样做过,文档也没有在路由器部分提到停用。查看代码,rootRouter.deactivate 函数上有以下注释:Disable history, perhaps temporarily. Not useful in a real app, but possibly useful for unit testing Routers.
  • 考虑在注销时使用window.location.reload()。这将从头开始 SPA。

标签: jquery twitter-bootstrap durandal


【解决方案1】:

我遇到了同样的错误,是的,您似乎必须调用 router.deactivate()。到目前为止,当我尝试做类似的事情时,我的页面上有以下内容:

function login() {
    router.reset();
    router.deactivate();
    app.setRoot('viewmodels/shell', 'entrance');
}

没有停用我会收到错误,但我不会。我认为重置或清空路由器会起作用,但没有。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,发现原因是我在映射路线后调用了.activate()方法[我的意思是在以外的其他地方调用activate() shell.js].

    总的来说,我可以说,如果您在 shell.js 以外的任何地方拥有它,只需 删除 .activate() p>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      相关资源
      最近更新 更多