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