durandal router.navigate不工作

durandal router.navigate不工作,durandal,durandal-navigation,durandal-2.0,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", tr

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

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);
}
router.navigate('destination');
))

在这里,我定义了我的moudle和路由器-目的地之间的映射,并将根视图设置为另一个视图位置选择器

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

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);
}
router.navigate('destination');

从开发人员工具中,我看到我的视图模型destination.js文件加载时没有错误,但视图根本没有改变。为什么会这样?顺便说一句,我使用的是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');
    });
}

我不知道这是否相关,但在启动应用程序之前激活路由器是非常不标准的。有什么原因吗?在我的代码中,我立即启动app.start并配置和激活我的路由器,直到承诺在随后的回调中得到解决。原因是我是一个初学者,一个愚蠢的人,:)因为我试图打破盒子,这对初学者来说当然是愚蠢的。我应该坚持使用StartKit,将shell视图用作根视图,然后在那里进行路由。打破这个框框很好,因为它可以帮助您理解代码在幕后真正做了什么。例如,我在这里所做的与初学者工具包大不相同:我在main中初始化了路由器,而不是shell,省略了router.buildnavigationmodel,并将activate链接到映射配置上。继续做好工作,并继续使用stackexchange上提供给您的资源:)