使用Meteor和Iron Router,我创建了使用多个参数的动态页面路径。但是,如果我尝试访问路径中的嵌套/子属性,则路由会中断。这些帖子很有帮助但没有涉及子属性:
Iron-router nested routes with multiple parameters
meteor iron-router nested routes
this.route('location',{
path: '/properties/:foo/:_id',
waitOn: function(){
return Meteor.subscribe('properties', this.params._id);
},
action: function(){
this.render('propertyPage', {
data: function(){
return Properties.findOne(this.params._id);
}
});
}
});
<a href="{{pathFor 'location' foo=bar }}">Click Me</a>
尝试在标记中引用嵌套属性时,它会断开:
<a href="{{pathFor 'location' foo=bar.nestedChild }}">Click Me</a>
我也在javascript中尝试过,没有运气:
path: '/properties/:foo.nestedChild/:_id',
有没有办法在不破坏铁路由器的情况下引用嵌套属性?
更实际的例子:
// data context from route action (Properties.findOne(this.params._id))
property = {
_id: "3cu7B8b6K3EzCgYnQ"
address: {
city: 'Houston',
state: 'TX',
zip: 77006,
lat: null,
lng: null
},
images: ['img1.png', 'img2.png', 'img3.png'],
schools: [
{ grade:'elementary', name:'Haude', rating:4 },
{ grade:'middle', name:'Strauke', rating:5 },
{ grade:'high', name:'Klein', rating:3 },
]
}
我正在尝试构建这样的url架构:
path: '/properties/:address.city/:address.state/:address.zip/:_id'
或在示例中:
"/properties/Houston/TX/77006/3cu7B8b6K3EzCgYnQ"
答案 0 :(得分:1)
在你的路线中,如果你想使用它,你需要从params对象中获取:foo :
var foo = this.params.foo;
答案 1 :(得分:0)
现在为时已晚,但无论如何有人可能会受益。我通过以下方式解决了它:
定义嵌套路径(BTW以这种方式定义路径对SEO更好)
<a href="/user/{{owner.id}}/playlist/{{id}}"> Content </a>
路由器
this.route('playlistItem', {
path: '/user/:owner/playlist/:playlist',
onBeforeAction: function() {
// You can get your params
var ownerId = this.params.owner
var playlistId = this.params.playlist
// execute some code
},
});