使用 Meteor,是否有可能拥有“动态路线"? [英] Using Meteor, is it possible to have "dynamic routes?"
问题描述
抱歉,我是 webdev 和 Meteor 的新手,我不太确定正确的术语.我正在使用 Meteor-Router 在我的 Meteor 应用中创建路线.
sorry, I'm new to webdev and Meteor and I'm not quite sure of the correct terminology. I am using Meteor-Router to create routes in my Meteor app.
我正在尝试创建一个测试餐厅应用,因此我的数据库中的条目可能是:
I'm trying to create a test restaurant app, so an entry in my database might be:
name: "Kentucky Fried Chicken"
type: "Fast Food"
在应用的主页上,您会看到一个餐厅列表.但是用户可以单击该列表中的任何项目以访问更详细的页面.
On the main page of the app, you see a list of restaurants. But the user can click on any item on that list to get to a more detailed page.
我宁愿网址看起来不像:
I would rather that the urls don't look like:
/restaurant/123
/restaurant/123
但更像是:
/fast-food/kentucky-fried-chicken
/japanese/sushi-r-us
/italian/some-italian-restaurant-name
这是否可以与 Meteor & 一起使用?流星路由器?谢谢!
Is this possible to do with Meteor & Meteor-Router? Thank you!
顺便说一句,现在我的路线很简单:
Btw, right now my routes are very simple:
Meteor.Router.add({
'/': 'home',
'/admin': 'admin',
'/403': 'unauthorized'
});
推荐答案
您可以使用比现在更复杂的路由,如下所示:
You can use more complex routes than the one you're using now, like this:
Meteor.Router.add({
'/:type/:restaurant': function(type, restaurantName) {
var restaurant = Retaurants.findOne({type: type, name: restaurantName});
Session.set('restaurantFromUrl', restaurant);
// Now your restaurant is in the "restaurantFromUrl" Session
return 'restaurantPage';
}
});
/:type 和/:restaurant 将被传递到回调中,并且是您在 URL 中设置的任何内容.哦,您可能还想添加一个/show-restaurant/type/name/,否则所有与模式/whatever/url"匹配的 url(未在其他路由中设置)都将尝试获取餐厅.
The /:type and /:restaurant will be passed into the callback and be whatever you set them to in your URL. Oh, and you might want to add a /show-restaurant/type/name/ also, else all urls (that aren't set up in other routes) that match the patter "/whatever/url" will try to get a restaurant.
您需要知道的一切都在这里:https://github.com/tmeasday/meteor-router
Everything you need to know is here: https://github.com/tmeasday/meteor-router
哦,这只是一个例子.还没有测试过,但它应该可以工作.
Oh, and this is just an example. Haven't tested it but it should work.
这篇关于使用 Meteor,是否有可能拥有“动态路线"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!