我曾經有類似的問題,我使用Router.sync
返回的承諾解決了(router.sync documentation)。呼叫Router.sync
在主視圖模型您.configure
路由器後,再打電話給你ko.applyBindings
,路由等在承諾的then
功能是這樣的:
oj.Router.rootInstance.configure(...);
oj.Router.sync().then(
function() {
// bind your ViewModel for the content of the whole page body.
ko.applyBindings(
new RootViewModel(),
document.getElementById('globalBody')
);
// navigate the user to login or wherever you want them to be now
oj.Router.rootInstance.go('login');
},
function (error) {
oj.Logger.error('Error in root start: ' + error.message);
}
);