2015-10-22 69 views
1

我是流星和铁路由器的新手。铁路由器的例子不是最新的,不能在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 
}); 

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

+0

铁路路由器是好的,但是,FlowRouter是Meteor生态系统的前进方向。 –

+0

http://meteor-guide.readthedocs.org/en/latest/routing/ –

+0

这是流量路由器的文档https://github.com/kadirahq/flow-router –

回答

1

流量路由器: -

确保你已经完成了...

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"}); 
    } 
}) 
+0

与你的例子,模板显示,但我仍然有错误:糟糕,看起来好像没有路由在客户端或服务器上的url:“http:// localhost:3000/two。” 。我不明白你的最后一个例子。您正在使用template1的模板在url templateOne上呈现模板布局,并且它仍然可以使用template2。 – BoumTAC

+0

不知道你的错误,第二个例子是你使用布局的地方....你可以将不同的模板渲染到该布局....所以如果你有一个菜单,取决于你在菜单中点击什么,你会更改不同的模板或许 –

+0

你用流量路由器帮了我很多,我认为我很好理解,但是我仍然遇到布局问题。你能检查出来吗? http://stackoverflow.com/questions/33356401/meteor-with-flow-router-layout-is-rendered-twice – BoumTAC

1

其实铁:路由器是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的评论re-flow-router。如果你刚刚开始,你可能想使用它。

+0

感谢它的工作,但仍然在控制台中得到一个错误:未捕获的错误:没有Iron.Layout发现,所以你不能使用yield !.你能告诉我同样的例子吗?或文档的链接? – BoumTAC

+0

当你添加铁:路由器到一个新的项目,铁:布局是包括在内。 但你可以单独添加它,'流星添加铁:布局' – JeremyK

+0

这里是一个[meteorpad示例](http://meteorpad.com/pad/YH9XPGh9KSKecbdZh/iron:router) – JeremyK