【发布时间】: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