【问题标题】:EmberJs router with two route segments with the same name - possible?具有两个同名路由段的 EmberJs 路由器 - 可能吗?
【发布时间】:2014-08-04 07:47:17
【问题描述】:

鉴于以下路由器实现:

Router.map(function() {
    this.resource('foo', function() { // URL /foo
        this.resource('bars', function() { //URL /foo/bars
            this.route('bar', { path: '/:bar_id', }); //URL /foo/bars/123
        });
    });

    this.route('bars'); //URL /bars

...我希望能够通过每行 cmets 中指示的 URL 访问它们。

出现问题是因为bars出现了两次; 当这种情况发生时,当 URL /foo/bars 被加载时, /bars 的模板和控制器在 /foo 的出口中呈现。

有没有办法解决这个问题,让我可以保持 URL 原样?

(即bars 出现在/foo/bars/bars 中)


更新:

这是基于@blessenm's suggestion 实现的解决方案:

Router.map(function() {
    this.resource('foo', function() { // URL /foo
        this.resource('foo-bars', { path: 'bars' }, function() { //URL /foo/bars
            this.route('bar', { path: '/:bar_id', }); //URL /foo/bars/123
        });
    });

    this.route('bars'); //URL /bars

为此,需要相应地修改保存各种 Ember 单元的文件路径:

  • app/{controllers,routes/templates}/foo-bars.{js,hbs}
  • app/{controllers,routes/templates}/foo-bars/bar.{js,hbs}

【问题讨论】:

    标签: javascript ember.js url-routing


    【解决方案1】:

    当您使用this.resource 时,您正在创建一个新的命名空间。这将干扰指定的最后一条路线。因此,要使最后一条路由正常工作,您需要更改其名称并指定 path 属性以获取所需的 url。更新后的代码如下所示

    App.Router.map(function() {
      this.resource('foo', function() { // URL /foo
        this.resource('bars', function() { //URL /foo/bars
          this.route('bar', { //URL /foo/bars/123
            path: '/:bar_id',
          });
        });
      });
    
      this.route('bars-root', {path: 'bar'});
    });
    

    Here is a working bin.

    【讨论】:

    • 感谢您的回答。我已经用实施的解决方案更新了my question,以及对文件路径的必要更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 2017-12-03
    相关资源
    最近更新 更多