Iron Router with multiple parts

自闭症网瘾萝莉.ら 提交于 2020-01-17 01:45:13

问题


I am using Meteor iron:router.

I have a template called by the name 'multi'. So whenever I have paths like '/multi' or '/multi/BSC123' , I want 'multi' to be rendered.

So far I have used an array approach like this

Router.route('multi',{
    path:['/multi','/multi/:_id']
});

This works fine. But when I use this approach, my left nav bar which has a href link to 'multi' template is not shown. So apart from the above approach, can anyone suggest me other solution where I can have two paths and same template rendered and I should get the "id" too if present.


回答1:


In your router js file, just create two routes that render the same template:

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

Router.route('/multi/:_id', function () {
  this.render('multi', {
    data: {
      routeid: this.params._id
    }
  });
});

In your multi template:

<template name="multi">
   Path: {{pathFor 'multi'}}<br />
   ID: {{routeid}}<br />
</template>

Now if you access /multi the Path shows up as 'multi' and the ID is empty. If you access /multi/BSC123 the Path shows up as 'multi' and the ID is BSC123.



来源:https://stackoverflow.com/questions/30505926/iron-router-with-multiple-parts

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