在durandal 2.0中定义起始模块并导航到它

Eli*_*eth 2 durandal

在我看来......这样的初始路线是通过以下方式定义的:

{ route: '', moduleId: 'viewmodels/customers', title: 'customers', nav: true },
Run Code Online (Sandbox Code Playgroud)

当应用程序加载''必须奇怪地设置为空的路由时,最初加载此路由.

当我现在导航到mysite /#/ customers时,没有任何内容被加载.

如何为我的路线提供一个起始模块,我可以用它来导航到它?

在旧路由器中我使用了startModule,但我在durandal 2.0中找不到它.

Rai*_*rit 6

您可能需要设置相同的第二条路线moduleId.这是一个使用这个http://dfiddle.github.io/dFiddle-2.0/#hellohttp://dfiddle.github.io/dFiddle-2.0的子路由的实例.

define(['plugins/router', 'durandal/system', 'global', 'knockout'], function( router, system, global, ko ) {
    var childRouter = router.createChildRouter()
      .makeRelative({
          moduleId: 'hello',
          route: 'hello'
      }).map([
          {route: '', moduleId: 'default/index', title: 'Hello World', type: 'intro'},
          {route: 'default', moduleId: 'default/index', title: 'Hello World', type: 'intro', nav: true},
          {route: 'dFiddle', moduleId: 'dFiddle/index', title: 'Hello World', type: 'fiddle', nav: true}
      ]).buildNavigationModel();

    // .on is mixed in an not meant to be  chainable 
    childRouter.on('router:navigation:complete').then(global.createSampleLink);

    return {
        global: global,
        router: childRouter,
        getItemsByCategoryId: function( categoryId ) {
            return ko.utils.arrayFilter(childRouter.navigationModel(), function( route ) {
                return route.type === categoryId;
            });
        },
        binding: function() {
            system.log('Lifecycle : binding : hello/index');
            return { cacheViews: false }; //cancels view caching for this module, allowing the triggering of the detached callback
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

这对所有顶级路线子路由该特定设置使用router.guardRouteshell.js处理空的根情况.有一个开放的票据https://github.com/BlueSpire/Durandal/issues/240讨论了更好地处理这类边缘情况.

define(['plugins/router'], function (router) {

    // Redirecting from / to first route
    router.guardRoute = function(routeInfo, params, instance){
        if (params.fragment === ''){
            return routeInfo.router.routes[0].hash;
        }
        return true;
    };

    return {
        router: router,
        activate: function () {
            router.map([
                { route: '', moduleId: 'hello/index', title: 'Hello World' },
                { route: 'hello*details', hash: '#hello', moduleId: 'hello/index', title: 'Hello World', nav: true },
                ...
            ]).buildNavigationModel();

            return router.activate();
        }
    };
});
Run Code Online (Sandbox Code Playgroud)