0

代码在这里:http://jsfiddle.net/cTpPx/,但在这里查看实时示例http://fiddle.jshell.net/cTpPx/show/,因为我希望你观察 url 的变化。

访问此页面时请禁用Chrome缓存(在内置Chrome调试器工具中勾选相关框,测试时不要选择调试器)

当您打开页面时,您会看到 hastag#!latest按预期更改,但在can.route.ready(true)执行时,它又变回#!空路由。为什么会有这种奇怪的行为?

但是,当您有此页面的缓存时,它将起作用。

4

1 回答 1

2

我很确定这是一个时间问题。您正在初始化控件并立即在路由更改事件中设置路由。如果您推迟设置哈希,它可以工作:http: //jsfiddle.net/cTpPx/2/

var AppController = can.Control({

    'route': function(){
        log('route empty');
        setTimeout(function() {
            window.location.hash = '#!latest';
        }, 10);
    },

    'latest route': function() {
        log('route: /latest');
    }

});
于 2013-04-30T14:08:14.467 回答