0

我试图弄清楚如何正确使用 createChildRouter。在兜了一天半试图找到能给我一些结果的代码之后,我暂时选择了以下代码(仍然没有给我结果):

外壳.js

var routes = [
      { route: '', moduleId: 'home', title: 'Home', nav: 1 },
      { route: 'inventory/*index', moduleId: 'inventory/inventory', title: 'Inventory', nav: 2 }];

return router.makeRelative({ moduleId: 'viewmodels' }) // router will look here for viewmodels by convention
      .map(routes)            // Map the routes
      .buildNavigationModel() // Finds all nav routes and readies them
      .activate();            // Activate the router

库存.js

define(['services/logger', 'plugins/router'], function (logger, router) {
      var title = 'Details';
      var childRouter = router.createChildRouter()
      .makeRelative({
         moduleId: 'viewmodels/inventory',
         fromParent: true
      }).map([
            { route: 'index', moduleId: 'inventory', title: 'Inventory', nav: 3 },
            { route: 'inventory', moduleId: 'items', title: 'Items', nav: 4 }])
         .buildNavigationModel();
      var vm = {
         router: childRouter,
         activate: activate,
         title: title
      };

      return vm;

      //#region Internal Methods
      function activate() {
         logger.log(title + ' View Activated', null, title, true);
      }
      //#endregion
});

我的树结构如下所示:

Durandal项目树

由于我没有得到任何结果,我开始怀疑问题不是我的路由代码,而是我的导航菜单或对子路由器应该如何/是否应该出现以及在没有额外工作的情况下可见的感知。我正在使用普通的香草模板(热毛巾),我唯一更改的是 router.js 中的日志记录代码,因为我试图弄清楚路由器代码正在寻找什么。当我单击按钮从“主页”视图切换到“库存”视图时,我从上述代码(使用修改后的日志记录)得到的结果如下:

LOG: /^$/ != inventory/*index 
LOG: /^inventory\/(.*?)$/ == inventory/*index 
LOG: Activating[object Object] 
LOG: [Details] Details View Activated 
LOG: Navigation Complete[object Object][object Object] 
LOG: /^index$/ != *index 
LOG: /^inventory$/ != *index 
LOG: Route '*index' Not Found 

所以我的问题是,这是路由代码应该做的吗?它是否应该无法将 splat 路线与任何子路线匹配?我需要做些什么来显示子路线的导航按钮吗?他们应该自动出现吗?我的视图确实切换到inventory.js/html,但发生这种情况时不会出现新的导航按钮。

我错过了什么?

4

1 回答 1

1

查看淘汰样本以了解子路由的工作实现。如果这是您所期望的,则不会将子路线添加到主导航中。它们旨在与辅助导航一起使用。HTML 版本可以在http://dfiddle.github.io/dFiddle-2.0/#knockout-samples上看到。

于 2013-11-13T16:02:02.717 回答