比方說,你有以下路由定義:鐵路由器如何確定路由優先級?
@route "home1",
path: "/:homeSlug1
和
@route "home2",
path: "/:homeSlug2
和
@route "home3",
path: "/fixedPath
,然後去localhost:3000/fixedPath
在瀏覽器
怎麼做鐵 - 路由器選擇優先級?
似乎固定路徑路線優先於參數化路線。這可能嗎?
比方說,你有以下路由定義:鐵路由器如何確定路由優先級?
@route "home1",
path: "/:homeSlug1
和
@route "home2",
path: "/:homeSlug2
和
@route "home3",
path: "/fixedPath
,然後去localhost:3000/fixedPath
在瀏覽器
怎麼做鐵 - 路由器選擇優先級?
似乎固定路徑路線優先於參數化路線。這可能嗎?
每當您撥打Router.route(...)
時,該新路線被推送到可能路線數組的後面。當請求進入時,IronRouter會遍歷該數組並選取第一個匹配的數組。因此,優先權直接來自您指定路線的順序。
一看source清楚:
Router.prototype.route = function (path, fn, opts) {
...
var route = new Route(path, fn, opts);
...
this.routes.push(route);
}
後來
Router.prototype.findFirstRoute = function (url) {
var isMatch;
var routeHandler;
for (var i = 0; i < this.routes.length; i++) {
route = this.routes[i];
// only matches if the url matches AND the
// current environment matches.
isMatch = route.handler.test(url, {
where: Meteor.isServer ? 'server' : 'client'
});
if (isMatch)
return route;
}
return null;
};
沒有優先權。匹配url表達式的第一條路由將被返回。 /:參數用於定義具有動態數據上下文的單個路由,通常是數據庫索引或唯一密鑰。