gpt4 book ai didi

javascript - 使用 Iron-router 的正则表达式路由

转载 作者:行者123 更新时间:2023-11-29 10:42:07 25 4
gpt4 key购买 nike

我正在尝试使用 iron-router 为 meteor 编写一条路由,以捕获对以下位置的所有请求:

  • /game/Quarterfinal3
  • /game/Semifinal5
  • /game/Grandfinal1

等这是我尝试过的方法,但它不起作用...

Router.route('final', {path: "(\\/)(game)(\\/).*?(final)(\\d)"}, function() {
console.log("final");
this.render('notStartedGame');
});

我该如何解决?

最佳答案

只要您在路径上使用 :pathParam 格式,您的路径中就不需要正则表达式,它将在 this.params.pathParam 中可用,即:

如果你想捕获这三个路线:

Router.route('/game/:gameName', function() {
var acceptablePaths = ["Quarterfinal3", "Semifinal5", "Grandfinal1"],
gameName = this.params.gameName;
// check if the game name is one of those that we are looking for
if ( _.contains(acceptablePath, gameName) ) this.render("notStartedGame");
// otherwise render a not found template or if you want do something else
this.render("gameNotFound"); //assuming you have such a template
});

或者如果您正在寻找其中包含“final”的任何路线:

Router.route('/game/:gameName', function() {
var checkFor = "final",
gameName = this.params.gameName;
// check if the game name includes "final"
if ( gameName.indexOf(checkFor) > -1 ) this.render("notStartedGame");
// otherwise render a not found template or if you want do something else
this.render("gameNotFound"); //assuming you have such a template
});

关于javascript - 使用 Iron-router 的正则表达式路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26844646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com