Meteor - 设置文档标题

Dea*_*age 41 meteor

有没有办法改变<title>Meteor应用程序中的元素?似乎模板仅在中处理<body>.

Dav*_*ihl 43

几乎在客户端JavaScript代码的任何地方:

document.title = "My new title";
Run Code Online (Sandbox Code Playgroud)


小智 38

您可以扩展David Wihl的解决方案:

Deps.autorun(function(){
  document.title = Session.get("DocumentTitle");
});
Run Code Online (Sandbox Code Playgroud)

然后你可以随时打电话:

Session.set("DocumentTitle","New Document Title");
Run Code Online (Sandbox Code Playgroud)


jrb*_*ard 12

如果你使用iron:router你可以添加包manuelschoebel:ms-seo来处理标题和meta标签.这对静态和动态SEO数据很有用:

Router.map(function() {
  return this.route('blogPost', {
    path: '/blog/:slug',

    onAfterAction: function() {
      var post = this.data().post;
      SEO.set({
        title: post.title,
        meta: {
          'description': post.description
        },
        og: {
          'title': post.title,
          'description': post.description
        }
      });
    }
  });
});
Run Code Online (Sandbox Code Playgroud)


Ber*_*nát 10

您可以创建一个帮助器来设置标题(CoffeeScript):

UI.registerHelper "setTitle", ->
  title = ""
  for i in [0..arguments.length-2]
    title += arguments[i]
  document.title = title
  undefined
Run Code Online (Sandbox Code Playgroud)

或在Js中相同:

UI.registerHelper("setTitle", function() {
  var title = "";
  for (var i = 0; i < arguments.length - 1; ++i) {
    title += arguments[i];
  }
  document.title = title;
});
Run Code Online (Sandbox Code Playgroud)

然后你可以用复杂的方式使用它,它会被反应:

{{#if book}}
  {{setTitle book.title}}
{{else}}
  {{setTitle "My books"}}
{{/if}}
Run Code Online (Sandbox Code Playgroud)


Jul*_*nec 7

我发现在路由器中直接处理这种事情比较方便onBeforeAction:

Router.map(function() {
  return this.route('StudioRoot', {
    path: '/',
    onBeforeAction: function() {
      return document.title = "My Awesome Meteor Application";
    }
  });
});
Run Code Online (Sandbox Code Playgroud)