有没有办法从 Meteor 中的模板访问 Iron Router 参数
Is there a way to access Iron Router parameter from template in Meteor
我有一个包含参数的路由,我需要从许多不同的模板访问它。下面是一个路由示例,有几条路由非常相似,只是在 _occasionnId 参数发生变化之后:
例如:
路线 1:/occasions/:_occasionId/cards/
路由器 2:/occasions/:_occasionId/tables/
这是我对每条路线的完整代码,唯一真正改变的是路线路径和模板。
Router.route('/occasions/:_occasionId/cards/', {
template: 'cards',
data: function(){
//var currentoccasion = this.params._occasionId;
//console.log(currentoccasion);
},subscriptions : function(){
Meteor.subscribe('cards');
Meteor.subscribe('tables');
}
});
我需要将 _occasionId 参数获取到一个模板中,该模板具有进入所有这些页面的导航。我的目标是从 Route 1 到 Router 2。但是我不知道如何在模板中添加正确的 URL。
我的模板是:
<template name="occasionnav">
<nav>
<div class="nav-wrapper">
<ul class="right hide-on-med-and-down">
<li><a href="/occasions/:_occasionId/cards/">cards</a></li>
<li><a href="/occasions/:_occasionnId/tables/">tables</a></li>
</ul>
</div>
</nav>
</template>
在“:_occasionId”的 'occasionnav' 模板中,我需要该参数与当前正在查看的页面的参数相同,并卡在此处。
如果有人对解决此问题的最佳方法有任何见解或建议,我将不胜感激。
您可以将 _occasionId 作为模板助手传递并在 jade 中呈现它,例如:
<li><a href="/occasions/{{_occasionId}}/cards/">cards</a></li>
你应该试试 :
Router.route('/occasions/:_occasionId/cards/', function() {
this.layout("LayoutName");
this.render("cards", {
data: {
currentoccasion = this.params._occasionId;
}
});
});
当您使用 Router.map 时,它的语法并不完全相同:
Router.map(function() {
this.route('<template name>', {
path: 'path/:_currentoccasion',
data: function () {
return {
currentoccasion: this.params._currentoccasion
}
}
});
您可以像在模板中那样访问 :
Like an helper
{{ currentoccasion }}
Or on the onRendered and onCreated functions
Template.<template name>.onRendered({
this.data.currentoccasion
如果您想在 Meteor 应用程序中呈现内部路由,我建议使用 {{pathFor}}
。
您只需要设置适当的上下文并命名您的路线,例如:
<template name="occasionnav">
<nav>
<div class="nav-wrapper">
<ul class="right hide-on-med-and-down">
{{#with occasion}}
<li><a href="{{pathFor route='occasions.cards'}}">cards</a></li>
<li><a href="{{pathFor route='occasions.tables'}}">tables</a></li>
{{/with}}
</ul>
</div>
</nav>
</template>
Router.route('/occasions/:_id/cards/', {
template: 'cards',
name: 'occasions.cards',
data: function() {
return Cards.findOne({_id: this.params._id});
},
subscriptions: function() {
return Meteor.subscribe('cards', this.params._id);
}
});
Router.route('/occasions/:_id/tables/', {
template: 'tables',
name: 'occasions.tables',
data: function() {
return Tables.findOne({_id: this.params._id});
},
subscriptions: function() {
return Meteor.subscribe('tables', this.params._id);
}
});
但是,您也可以通过 Router.current().params
在您的模板中获取路由器参数。
我有一个包含参数的路由,我需要从许多不同的模板访问它。下面是一个路由示例,有几条路由非常相似,只是在 _occasionnId 参数发生变化之后:
例如:
路线 1:/occasions/:_occasionId/cards/
路由器 2:/occasions/:_occasionId/tables/
这是我对每条路线的完整代码,唯一真正改变的是路线路径和模板。
Router.route('/occasions/:_occasionId/cards/', {
template: 'cards',
data: function(){
//var currentoccasion = this.params._occasionId;
//console.log(currentoccasion);
},subscriptions : function(){
Meteor.subscribe('cards');
Meteor.subscribe('tables');
}
});
我需要将 _occasionId 参数获取到一个模板中,该模板具有进入所有这些页面的导航。我的目标是从 Route 1 到 Router 2。但是我不知道如何在模板中添加正确的 URL。
我的模板是:
<template name="occasionnav">
<nav>
<div class="nav-wrapper">
<ul class="right hide-on-med-and-down">
<li><a href="/occasions/:_occasionId/cards/">cards</a></li>
<li><a href="/occasions/:_occasionnId/tables/">tables</a></li>
</ul>
</div>
</nav>
</template>
在“:_occasionId”的 'occasionnav' 模板中,我需要该参数与当前正在查看的页面的参数相同,并卡在此处。
如果有人对解决此问题的最佳方法有任何见解或建议,我将不胜感激。
您可以将 _occasionId 作为模板助手传递并在 jade 中呈现它,例如:
<li><a href="/occasions/{{_occasionId}}/cards/">cards</a></li>
你应该试试 :
Router.route('/occasions/:_occasionId/cards/', function() {
this.layout("LayoutName");
this.render("cards", {
data: {
currentoccasion = this.params._occasionId;
}
});
});
当您使用 Router.map 时,它的语法并不完全相同:
Router.map(function() {
this.route('<template name>', {
path: 'path/:_currentoccasion',
data: function () {
return {
currentoccasion: this.params._currentoccasion
}
}
});
您可以像在模板中那样访问 :
Like an helper
{{ currentoccasion }}
Or on the onRendered and onCreated functions
Template.<template name>.onRendered({
this.data.currentoccasion
如果您想在 Meteor 应用程序中呈现内部路由,我建议使用 {{pathFor}}
。
您只需要设置适当的上下文并命名您的路线,例如:
<template name="occasionnav">
<nav>
<div class="nav-wrapper">
<ul class="right hide-on-med-and-down">
{{#with occasion}}
<li><a href="{{pathFor route='occasions.cards'}}">cards</a></li>
<li><a href="{{pathFor route='occasions.tables'}}">tables</a></li>
{{/with}}
</ul>
</div>
</nav>
</template>
Router.route('/occasions/:_id/cards/', {
template: 'cards',
name: 'occasions.cards',
data: function() {
return Cards.findOne({_id: this.params._id});
},
subscriptions: function() {
return Meteor.subscribe('cards', this.params._id);
}
});
Router.route('/occasions/:_id/tables/', {
template: 'tables',
name: 'occasions.tables',
data: function() {
return Tables.findOne({_id: this.params._id});
},
subscriptions: function() {
return Meteor.subscribe('tables', this.params._id);
}
});
但是,您也可以通过 Router.current().params
在您的模板中获取路由器参数。