Durandal路由器在哪里设置页面标题

Where does Durandal router set page title

本文关键字:标题 设置 路由器 在哪里 Durandal      更新时间:2023-09-26

我正在使用Durandal为一个非常简单的网站。在我所有的浏览器选项卡中,页面标题都出现了"未定义|"附加到应用程序标题的前面。Durandal是从哪里获得并设定这个值的?

pthalacker

最终Durandal的路由器插件正在设置document.title.

https://github.com/dfiddle/dfiddle - 1.2/- blob/gh pages/app/durandal/plugins/router.js # L254

onNavigationComplete: function (routeInfo, params, module) {
    if (app.title) {
        document.title = routeInfo.caption + " | " + app.title;
    } else {
        document.title = routeInfo.caption;
    }
},...

通常Durandal能够在route对象上构造一个缺失的标题属性,所以可能路由的设置方式有所不同。

https://github.com/dfiddle/dfiddle - 1.2/- blob/gh pages/app/samples/shell.js # 16种

router.map([
   { url: 'hello', moduleId: 'samples/hello/index', name: 'Hello World', visible: true },
   { url: 'hello/:name', moduleId: 'samples/hello/index', name: 'Examples' },...
]);

您可以在视图模型被激活时设置页面标题:

activate: function (params) {
    // Setting page title
    params.routeInfo.caption = "My Page Title";
    return true;
}

在Durandal 2.1.0中替换页面标题

(如果你想让页面标题不同于路由的最后一层)

对RainerAtSpirit的回答做了一个小修改:在路由中指定'title'而不是'name'。

router.map([
   { url: 'hello', moduleId: 'samples/hello/index', title: 'Hello World', visible: true },
   { url: 'hello/:name', moduleId: 'samples/hello/index', title: 'Examples' },...
]);