简体   繁体   English

将div链接到路由流星(铁路由器)

[英]Link a div to a route meteor (Iron Router)

I would like to "associate a route to a div" with Iron Router without refreshing/changing page. 我想在不刷新/更改页面的情况下使用Iron Router“将路由关联到div”。 I'll try to be as clear as possible. 我会尽量清楚。

I got a div that's not displayed when you arrive in the page (ex: /home ), but already exists. 当您到达页面(例如: / home )时,我得到了一个未显示的div,但它已经存在。

When you click on something (box, text, ...), the url changes ( /home/box1 ) and the div appears with some data I'd have given to the route before. 单击某些内容(框,文本,...)时,URL会更改( / home / box1 ),并且div会显示一些我之前给定的数据。 But I would like to be able to do that without having to refresh the page, just to see the div appears in the same page. 但是我希望能够做到这一点而不必刷新页面,只是要看到div出现在同一页面中。

So, is it possible with Iron Router and how ? 那么,铁路由器有可能吗?

Thanks for your help. 谢谢你的帮助。

If you are trying to display the same template on both routes, but only show the div on one of them, you can pass data into the template. 如果您试图在两条路线上显示相同的模板,但仅在其中一条路线上显示div,则可以将数据传递到模板中。 The render method takes a second argument which is an object. render方法采用第二个参数,它是一个对象。 You set the data property on that object to be a function that returns an object with all of your data values. 您可以将该对象的data属性设置为一个函数,该函数返回具有所有数据值的对象。 You can then access them from the template as if they had been defined when inserting the template. 然后,您可以从模板访问它们,就像在插入模板时已定义它们一样。

 Router.route('/home', function() { this.render('Home'); }); Router.route('/home/box1', function() { this.render('Home', { data: function() { return { showDiv: true } } } }); 
 <head> </head> <body> </body> <template name="Main"> <p>Always showing</p> {{#if showDiv}} <p>Only showing when on /home/box1 route</p> {{/if}} </template> 

Note: This will not preserve the page you are on regardless of where you are. 注意:无论您身在何处,这都不会保留您所在的页面。 IE If you wanted to do this on the /other and /other/box1 routes. IE如果您想在/ other和/ other / box1路由上执行此操作。 If this is the behavior you're looking for I can help you with that as well. 如果这是您要寻找的行为,我也可以帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM