【问题标题】:Ember route with dynamic segment name具有动态分段名称的 Ember 路由
【发布时间】:2015-11-22 18:09:53
【问题描述】:

我刚开始使用 Ember JS 和 Ember CLI 并试图找出这个路由问题。我有一个包含许多游戏模型的组模型。通过以下路线,我可以很好地从组 URL 显示游戏:

Router.map(function() {
  this.resource("groups", function() {
    this.route('show', {path: ':group_id/show' });
  });
});

这会产生一个格式如下的 URL:

http://localhost:4200/groups/1/show

假设其中一个组名是“wizards”。我希望能够以以下形式构造一个 URL 并渲染所有属于“wizards”的游戏:

 http://localhost:4200/wizards

感谢任何提示。

【问题讨论】:

  • 为什么不添加这样的新路由。route('group', {path: ':group_name' });在组资源中。

标签: ember.js ember-data ember-cli


【解决方案1】:

就像@blessenm 在 cmets 中指出的那样,您的路由器将从

Router.map(function() {
  this.resource("groups", function() {
    this.route('show', {path: ':group_id/show' });
  });
});

Router.map(function() {
  this.resource("group", { path: ':group_name'});
});

this.resource()this.route() 的第二个参数是可选的。如果您没有传递任何东西 - 它假定与您的路线/资源(在您的情况下为组)相同的名称。如果您传入具有 path: 键的对象 - 您正在指定路由的 url 是什么,包括动态段。有关这方面的 Ember 文档,请参阅 here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多