【问题标题】:Durandal routing issueDurandal 路由问题
【发布时间】:2013-05-16 16:02:27
【问题描述】:

我有 Durandal 的小型 SPA 测试应用程序。 我也有非常有线的问题。 首先,我的文件夹结构是:
应用
--durandal
--viewmodels
----user.js
--views
----user.html
--main.js
当结构是这样时,一切都很好。但是如果我创建像
这样的结构 应用程序
--durandal
--_用户
----视图模型
------user.js
----浏览量
------user.html

我收到类似 localhost/App/_users/viewmodels/users.html 404 Not Found 的错误。这发生在 user.js 被 require.js 加载之后。

我的 main.js 看起来像

require.config({
   paths: { "text": "../durandal/amd/text" }
});

define(function (require) {
   var system = require('../durandal/system'),
      app = require('../durandal/app'),
      router = require('../durandal/plugins/router'),
      viewLocator = require('../durandal/viewLocator'),
      logger = require('../logger');

   system.debug(true);

   app.start().then(function () {
      // route will use conventions for modules
      // assuming viewmodels/views folder structure
      router.useConvention();


      // When finding a module, replace the viewmodel string 
      // with view to find it partner view.
      // [viewmodel]s/sessions --> [view]s/sessions.html
      // Otherwise you can pass paths for modules, views, partials
      // Defaults to viewmodels/views/views. 
      viewLocator.useConvention();

      app.setRoot('viewmodels/shell');

      // override bad route behavior to write to 
      // console log and show error toast
      router.handleInvalidRoute = function (route, params) {
         logger.logError('No route found', route, 'main', true);
      };
   });
});

我认为这个问题与 router.useConvention();或使用 viewLocator.useConvention();但简单找不到这种行为的任何原因。

任何帮助、建议、想法如何解决这个问题?

谢谢

【问题讨论】:

    标签: router durandal


    【解决方案1】:

    这是因为 view locator 的行为,默认情况下会在您描述的第一个结构中查找视图/视图模型。

    您可以通过提供自己的视图定位器函数或通过像这样useConvention(modulesPath, viewsPath, areasPath) 调用useConvention() 来轻松更改此行为

    【讨论】:

    • 谢谢,但是 useConvention() 或不起作用,或者我只是不知道该放什么。如果我把类似 useConvention('_users/viewmodels', '_users/views') 然后 app.setRoot('viewmodels/shell');不起作用,然后我收到错误 404 Not Found - localhost/App/_users/viewmodels/shell.html"。如果我只输入 .useConvention('viewmodels', 'views');,然后再次获取 localhost/App/ _users/viewmodels/users.html 404 Not Found。所以,这非常令人困惑,如果我将 user.html 从视图文件夹移动到视图文件夹,那么所有分叉。但得到的调试消息略有不同。
    • 使用useConvention('_users/viewmodels', '_users/views', '_users/views')app.setRoot('_users/viewmodels/shell')。那应该行得通。请记住,任何时候您引用视图模型时,您仍然必须使用 full moduleid 路径,它是来自 App 文件夹的路径。
    • 谢谢,但这仍然不起作用。当我应用您的建议时,我得到“NetworkError:404 Not Found - localhost/App/_users/_users/viewmodels/shell.js”
    • 我刚刚在您的第一条评论中注意到 404 正在寻找 viewmodels/shell.html 显然不会存在,它应该在 views 中寻找 html。你确定不是第一次切换参数?你能再试试useConvention('_users/viewmodels', '_users/views', '_users/views') then app.setRoot('viewmodels/shell')吗?
    猜你喜欢
    • 1970-01-01
    • 2013-05-21
    • 2018-04-19
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多