微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 使用iron-router时设置HTML标题

如何在使用铁路由器时最好地设置 HTML标题?这是我想做的事情:
<template name="layout">
    <head><title>{{KAZOOM}}</title></head>
    <body>
        {{> menu}}
        {{yield}}
    </body>
</template>

<template name="example">
    {{KAZOOM 'example page'}}
    That's just an example page
</template>

<template name="foo">
    {{KAZOOM 'foo page'}}
    Another example page with different HTML title
</template>

你看到KAZOOM如何及时回归来设置HTML标题?我希望这样做的原因是我认为HTML标题内容的一部分.我可以通过编辑生成它的模板来调整页面的HTML标题.不幸的是,我没有看到实现这一目标的干净方法.我能想到的最接近的是命名率,然后标题将由路径设置,而不是由模板设置.

另一种可能性是放弃布局模板并始终包含标题

<template name="head">
    <head><title>{{this}}</title></head>
    {{> menu}}
</template>

<template name="example">
    {{> head 'example page'}}
    That's just an example page
</template>

<template name="foo">
    {{> head 'foo page'}}
    Another example page with different HTML title
</template>

那不是很好.你有适当的解决方案吗?

解决方法

在Iron路由器中设置document.title onAfterRun:
var pageTitle = 'My super web';
Router.map(function() {
   this.route('user',{
      onAfterRun: function() {
        document.title = 'User ' + this.params.name + ' - ' + pageTitle;
      }
   });
});

编辑:

如果要在模板中设置标题,请创建自定义Handlebars帮助程序(客户端代码):

Handlebars.registerHelper("KAZOOM",function(title) {
    if(title) {
        document.title = title;
    } else {
        document.title = "Your default title";
    }
});

并在使用它时在模板中使用它

{{KAZOOM 'example page'}}

要么

{{KAZOOM}}

标题.

编辑2015年7月26日:对于新的铁路由器,它看起来像:

Router.route('/user',{
  onAfteraction: function() {
    document.title = 'page title';
  }
});

原文地址:https://www.jb51.cc/js/156639.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐