我需要从代码中的事件处理程序导航到另一个 View ,我这样做
define(['durandal/system', 'durandal/app', 'durandal/viewLocator', 'plugins/router', 'underscore'],
function (system, app, viewLocator, router, _) {
system.log('starting app');
//>>excludeStart("build", true);
system.debug(true);
//>>excludeEnd("build");
app.title = 'Destiny';
app.configurePlugins({
router: true,
dialog: true,
widget: true,
observable: true
});
router.map('destination', 'viewmodels/destination');
router.activate();
_.delay(function() {
app.start().then(function() {
//Replace 'viewmodels' in the moduleId with 'views' to locate the view.
//Look for partial views in a 'views' folder in the root.
viewLocator.useConvention();
//Show the app by setting the root view model for our application with a transition.
app.setRoot('viewmodels/locationPicker', 'flip');
});
}, 1500);
}
);
这里我定义了我的moudle和路由器-目的地之间的映射,并将 Root View 设置为另一个 View locationPicker
在我的另一个 View locationPicker.js 中,我像这样导航到它
router.navigate('destination');
从开发人员工具中,我看到我的 View 模型destination.js 文件加载时没有错误,但 View 根本没有改变。为什么会发生这种情况?顺便说一句,我使用的是 durandal 2.0.1 版本
最佳答案
路由器插件在调用app.start时初始化。因此,您在初始化之前配置插件,并且配置未注册。另外,我不熟悉您注册路线的语法。更标准的方法是传入带有路由模式和模块 ID 的对象列表。请尝试以下操作:
define(['durandal/system', 'durandal/app', 'durandal/viewLocator', 'plugins/router'],
function (system, app, viewLocator, router) {
system.log('starting app');
//>>excludeStart("build", true);
system.debug(true);
//>>excludeEnd("build");
app.title = 'Destiny';
app.configurePlugins({
router: true,
dialog: true,
widget: true,
observable: true
});
app.start().then(function() {
router.map([
{ route: 'destination', moduleId: 'viewmodels/destination' }
]).activate();
//Replace 'viewmodels' in the moduleId with 'views' to locate the view.
//Look for partial views in a 'views' folder in the root.
viewLocator.useConvention();
//Show the app by setting the root view model for our application with a transition.
app.setRoot('viewmodels/locationPicker', 'flip');
});
}
关于durandal router.navigate 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19270153/