在流星中处理多个"页面"的正确方法

Jos*_*ill 9 meteor

在流星中处理多个"页面"的"正式"方式是什么?我说"页面"我看到人们用不同的方式做到这一点.我见过人们创建实际的完整页面(index.html,about.html,contact.html),然后点击链接时,你会写一个路由来渲染这些页面.但我也看到人们基本上将每个页面的代码放在<template>标签中,然后根据他们点击的内容,登录凭据等做了漂亮的显示/隐藏类型的东西.

Raj*_*d02 17

有几种方法可以处理流星中的多个页面

1.iron路由器

使用铁路由器,您可以创建一个布局模板,并使用其中的所有其他模板注入{{> yield}}.检查铁路由器指南以了解有关布局模板的更多信息.

2.{{> Template.dynamic}}

如果你不想添加铁路由器,你可以{{> Template.dynamic}}用来达到同样的效果.

<body>
  <ul>
    <li><a href="#" class="index">Home</li>
    <li><a href="#" class="about">About</li>
    <li><a href="#" class="contact">Contact</li>
  </ul>
  {{> Template.dynamic template=template_name }}
</body>
Run Code Online (Sandbox Code Playgroud)

template_name 可以使用模板助手进行反应性更改

Meteor.startup(function () {
  Session.setDefault("templateName", "index")
});

Template.body.helpers({
  template_name: function(){
    return Session.get("templateName")
  }
});

Template.body.events({
  "click .home": function() {
    Session.set("templateName", "index");
  },
  "click .about": function() {
     Session.set("templateName", "about");
  }
  // ..
});
Run Code Online (Sandbox Code Playgroud)

如果Session返回"about"那么, {{> Template.dynamic template="about"}}相当于{{> about}}.