durandal router.navigate 不工作

标签 durandal durandal-navigation durandal-2.0

我需要从代码中的事件处理程序导航到另一个 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/

相关文章:

javascript - 如何将 SignalR 包含在 gulp 构建过程中?

knockout-2.0 - 模板的Durandal递归组合

javascript - 访问某个页面两次后数据不显示

javascript - knockout 无容器 'with' 绑定(bind)不起作用

durandal - Durandal 中的条件路由

javascript - 在对服务器的调用完成之前启动屏幕消失

durandaljs - 单击按钮导航查看

javascript - Durandal js路由器设置

javascript - Durandal路由IF语句

widget - Durandal 小部件 "this.settings is undefined"