替换 %20,表示铁路由器上的空格

Replace the %20 that means a space on iron router

本文关键字:空格 路由器 表示 替换      更新时间:2023-09-26

我正在尝试将表示网址上的空格的%20替换为另一个字符,

Router.map(function () {
  this.route('promos', {
    path: '/:promo_name',
    waitOn: function(){
        return Meteor.subscribe("promo", this.params.promo_name);
    },
    data: function(){
      return Promociones.findOne({'metadata.nombrePromo': this.params.promo_name});
    }
  });
});

这就是我生成动态路由的方式,http://padonde.mx/Pi%C3%B1a%20Colada%202x1 我想用另一个字符替换 %20 ,我会得到这样的东西,例如 -+,这在铁路由器上可能吗?

这实际上是一个标准的 URL 编码,它由浏览器设置不是铁路由器。

你可以这样做,

插入

'metadata.nombrePromo', add another field to the collection like

'metadata.nombrePromoReplace',然后这样做。我猜你有类似的东西

var nombrePromo = $('#idElementPromo').val(); ,或session或任何将值传递给元数据的内容

因此,基于该VaR nombrePromo这样做。

var nombrePromoReplace = nombrePromo.replace(/'s+/g, '');

现在更改路线。

Router.map(function () {
  this.route('promos', {
    path: '/:promo_name',
    waitOn: function(){
        return Meteor.subscribe("promo", this.params.promo_name);
    },
    data: function(){
      return Promociones.findOne({'metadata.nombrePromoReplace': this.params.promo_name});
    }
  });
});

现在,当您导航到/:p romo_name 时,如果您添加了类似

i have blanks spaces

路线应该是

/ihaveblankspaces

它应该有效。

我不知道任何铁路由器配置可以改变这一点,但您可以执行以下操作:

添加名为"集合助手"的小包:

$ meteor add dburles:collection-helpers

然后在集合中定义一个虚拟字段(例如:polished_promo_name):

Promociones.helpers({
  polished_promo_name: function() {
    return this.promo_name.replace(/ /g, "AnotherCharacter");
  }
});

现在当然使用polished_promo_name而不是promo_name并将空格替换回:

Router.map(function () {
  this.route('promos', {
    path: '/:polished_promo_name',
    waitOn: function(){
      return Meteor.subscribe("promo", this.params.polished_promo_name.replace(/AnotherCharacter/g, " "));
    },
    data: function(){
      return Promociones.findOne({'metadata.nombrePromo': this.params.polished_promo_name.replace(/AnotherCharacter/g, " ")});
    }
  });
});