【问题标题】:make route with iron router meteor用铁路由器流星制作路线
【发布时间】:2016-01-22 08:35:46
【问题描述】:

我是流星和铁路由器的新手。 Iron 路由器示例不是最新的,不能在 github 上运行。 我只想做一个简单的路线。 这是我的 /client/index.html

 <html>
  <head>
    <title></title>
  </head>
  <body>

    {{> template1OrTemplate2 depending on url}}
  </body>
</html>


<template name="template1">
  one
</template>

<template name="template2">
  two
</template>

我的 /lib/router.js:

Router.route('/templateOne', function () {
  // renderMyTemplate1 please
});

Router.route('/templateTwo', function () {
  // renderMyTemplate2 please
});

这么容易的东西怎么可能这么难找?

【问题讨论】:

标签: javascript meteor iron-router router


【解决方案1】:

实际上,iron:router 是well documented,这和你想象的一样微不足道。

<head>
  <title></title>
</head>
<body>
  {{> yield}}
</body>


<template name="template1">
  one
</template>

<template name="template2">
  two
</template>



Router.route('/templateOne', function () {
  this.render('template1')
});

Router.route('/templateTwo', function () {
  this.render('template2')
});

但是我同意 Keith 的 cmets re flow-router。如果您刚刚开始,您可能想改用它。

【讨论】:

  • 感谢它工作,但控制台中仍然出现错误:未捕获错误:未找到 Iron.Layout,因此您不能使用 yield!。您也可以向我展示与 flowrouter 相同的示例吗?还是文档的链接?
  • 当你将 iron:router 添加到一个新项目时,iron:layout 会包含在内。但是你可以单独添加,meteor add iron:layout
【解决方案2】:

对于流路由器:-

确保您已经完成...

meteor add kadira:flow-router kadira:blaze-layout

然后

FlowRouter.route('/templateOne', {
    action() {
        BlazeLayout.render("template1");
    }
}) 

FlowRouter.route('/templateTwo', {
    action() {
        BlazeLayout.render("template2");
    }
}) 

使用布局,您可以做类似的事情

<template name="layout">
  <div>My App</div>
   {{>Template.dynamic template=content}}
  </template>

然后

FlowRouter.route('/templateOne', {
    action() {
        BlazeLayout.render("layout", {content:"template1"});
    }
}) 

【讨论】:

  • 在您的示例中,模板已显示,但我仍然收到错误:糟糕,客户端或服务器上似乎没有 url 的路由:“localhost:3000/two”。 .我不明白你的最后一个例子。您正在使用 template1 的内容在 url templateOne 上呈现模板布局,并且它仍在使用 template2。
  • 不确定你的错误,第二个例子是你使用布局的地方......你可以将不同的模板渲染到那个布局中......所以如果你有一个菜单,这取决于你点击菜单中的 ,您可能会修改不同的模板
  • 您在流量路由器方面帮了我很多忙,我以为我理解得很好,但布局仍然有问题。你能检查一下吗? stackoverflow.com/questions/33356401/…
猜你喜欢
  • 2014-09-03
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 2015-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多