Proper way to handle multiple “pages” in meteor

空扰寡人 提交于 2019-12-18 13:23:40

问题


What is the "formal" way of handling multiple "pages" in meteor? I say "pages" I've seen people do it a couple of different ways. I've seen people create actual full pages, (index.html, about.html, contact.html) and then when links are clicked, you'd write a route to render those pages. But I've also seen people essentially put the code for each of those pages inside <template> tags and then do nifty show/hide type stuff based of what they've clicked, login credentials, etc.


回答1:


There are several ways to handle multiple pages in meteor

1.iron-router

using iron router you can create a layout template and inject all other templates inside it using {{> yield}}. Check the iron-router guide to know more about layout template.

2.{{> Template.dynamic}}

if you do not want to add iron-router you can use {{> Template.dynamic}} to achieve the same.

<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>

template_name can be changed reactively using a template helper

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");
  }
  // ..
});

If the Session returns "about" then, {{> Template.dynamic template="about"}} which is equivalent to {{> about}}.



来源:https://stackoverflow.com/questions/26944011/proper-way-to-handle-multiple-pages-in-meteor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!