javascript - 带参数的 Durandal 路由

标签 javascript jquery durandal

我有一个 Durandal 应用程序,其路由如下。

activate: function () {
    return router.map([{
        route: 'friends',
        moduleId: 'viewmodels/friends',
        nav: true
    }, {
        route: 'about',
        moduleId: 'viewmodels/about',
        nav: true
    }, {
        route: '(:id)',
        title: 'TimeLine',
        moduleId: 'viewmodels/Posts',
        nav: true
    }, ]).buildNavigationModel()
        .activate();

但是每次我们导航到“ friend ”或“关于”时,它都会转到“viewmodel/Post”,其中“ friend ”和“关于”关键字是“id”。我无法转到“viewmodels/ friend ”和“viewmodels/about”。有人可以帮助我吗?

最佳答案

确保在 viewmodels/ 中您有一个名为 posts.js 的文件,并且您的 moduleId 不是 moduleId: 'viewmodels/posts' moduleId: 'viewmodels/Posts'posts.js 必须相同

将代码更改为:

activate: function () {
    return router.map([{
        route: ['', 'friends'],//this is the home route ['', 'routeName']
        moduleId: 'viewmodels/friends',
        nav: true
    }, {
        route: 'about',
        moduleId: 'viewmodels/about',
        nav: true
    }, {
        route: 'posts/(:id)',// and change this from (:id) to posts/:id or posts/(:id)
        title: 'TimeLine',
        moduleId: 'viewmodels/posts',
        nav: true
    }, ]).buildNavigationModel()
        .activate();

在您的 posts.js 中,您可以像这样访问 id

var postId;
function activate(id) {
        postId = id;
        logger.log(viewModel.title + "'s  View Activated", null, viewModel.title, true);
        return true;
    }

function attached(view, parent) {
        alert(postId);
}

var viewModel = {
        activate: activate,
        title: 'singlePost',
        attached: attached
}

return viewModel;

了解更多关于 Mapping Routes

关于javascript - 带参数的 Durandal 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24572515/

相关文章:

javascript - 使用JavaScript从客户端浏览器上传到Google Cloud Storage

javascript - Javascript 的文件大小因位置、本地或服务器而异

javascript - jQuery inArray 只查看第一个值

在 Hot Towel 应用程序中使用 Knockout 可观察到绑定(bind)

javascript - 如何在 QML Javascript 中创建和使用 C++ 对象

javascript - 如何使用 requireJS 从另一个文件调用函数

javascript - 查询 : Wait for the toastr notification to end then reload

javascript - Quill文本编辑器使用JQuery创建后如何使用实例

android - Phonegap android 应用程序使用 durandal : page with ajax call not opening

model-view-controller - knockoutjs 的无容器语句在 hottowel SPA 中不起作用?