【问题标题】:Optional parameters in Meteor routerMeteor路由器中的可选参数
【发布时间】:2015-07-14 18:55:33
【问题描述】:

我在 Meteor 中有一条路径

path: '/my-url/:groupId?',

我添加了问号以表明有时groupId 未被使用。在data 中,我检查组ID 是否设置为this.params.hasOwnProperty('groupId'),但我发现有时路由器认为groupId 已设置,即使它不是(不知道为什么)但值为undefined

因此,我尝试了

console.log(this.params.hasOwnProperty('groupId'));
console.log('groupId' in this.params);
console.log(this.params.groupId);

他们评估为

> true
> true
> undefined

所以我猜hasOwnProperty 不是检查是否设置了groupId 的最佳方法,因为它不检查未定义的值。

有什么更好的方法来检查这个?即使我的网址是 /my-url,为什么 hasOwnProperty 评估为 true?

【问题讨论】:

  • 你找到其他解决方案了吗

标签: javascript regex node.js meteor iron-router


【解决方案1】:

我认为您已经回答了自己的问题。我有同样的问题,我的检查是:

if (this.params.groupId) {
} else {
}

这还没有得到证实,但我认为只要你提供:groupId,你就已经在参数中拥有它,不管它是否存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 2019-09-14
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多