durandal router.navigate无法正常工作

时间:2013-10-09 10:58:27

标签: durandal durandal-navigation durandal-2.0

我需要从代码中的事件处理程序导航到另一个视图,我这样做

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和路由器之间的映射 - 目的地和设置根视图作为另一个视图locationPicker

在我的另一个视图locationPicker.js中,我像这样导航到它

router.navigate('destination');
从开发人员工具

,我看到我的视图模型destination.js文件加载没有错误,但视图根本没有改变。为什么会这样?顺便说一下,我正在使用durandal 2.0.1版本

1 个答案:

答案 0 :(得分:2)

路由器插件在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');
    });
}
相关问题