【问题标题】:Custom URL in MeteorMeteor 中的自定义 URL
【发布时间】:2016-06-07 22:21:11
【问题描述】:

我的项目中有这样的网址:https://localhost:3000/groups/KhHB9TwzZxiddgJND/event

我想得到这样的想法:https://localhost:3000/groups/groupName/event

我该怎么做?我在 Blaze 上使用 Meteor 1.3,用于路由的流路由器

【问题讨论】:

    标签: javascript url meteor meteor-blaze flow-router


    【解决方案1】:

    您只需定义路线并使用name 变量而不是id

    FlowRouter.route('/groups/:name/event', {});
    

    然后当您想为模板设置数据上下文时,您可以执行以下操作:

    let groupName = FlowRouter.getParam("name");
    const group = Groups.findOne({ name: groupName });
    

    _id 在路由中并不特殊,路由参数只是您最终要搜索的东西。 _id 的好处是它可以保证是独一无二的。如果您按名称搜索,那么您可能必须在模型中强制执行唯一性,除非您真的想要一个列表。

    【讨论】:

    • 它有效,但我怎样才能在 URL 中获得 ' ' ?我得到这个:http://localhost:3000/groups/My%40Group/event 并想要这个:http://localhost:3000/groups/My_Group/event。 URL 中没有符号代码。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2015-02-20
    相关资源
    最近更新 更多