【问题标题】:Ember JS - routing from resourse to routeEmber JS - 从资源路由到路由
【发布时间】:2018-06-25 11:49:30
【问题描述】:

我正在尝试将项目从 2.x 版更新到 3.x 版,我的第一个问题是在路由文件中我得到 this.resouce 未定义。我相信这已被弃用。代码如下所示:

  this.route('objects', function () {
    this.route('search', { path: '/' }, function () {
      this.resource('objects.items', { path: '/:search_id' }, function () {
        this.resource('objects.item', { path: '/item/:item_id'}, function () {
          this.route('general', { path: '/' });
          this.route('tab', { path: '/tab/:tab' });
          this.route('relations');
          this.route('diagram');
          this.route('comments');
          this.route('sources');
          this.route('views');
        });
      });
    });
  });

我尝试简单地将资源更改为路由并赋予 resetNameSpace: true 属性,但没有帮助。我也尝试了各种组合,但没有运气。 谁有经验可以帮我重做这个路由以兼容最新的ember?

【问题讨论】:

  • “但没有运气”是什么意思?什么不起作用?
  • 我用过的例子:this.route('objects', function () { this.route('search', { path: '/' }); this.route('items', { path: '/:search_id', resetNameSpace: true }); this.route('item', { path: '/item/:item_id' }, function () { this.route('general', { path: '/' }); this.route('tab', { path: '/tab/:tab' }); this.route('relations'); this.route('diagram'); this.route('comments'); this.route('sources'); this.route('views'); }); });,
  • 然后我得到“你试图定义一个{{link-to "objects.item"}},但没有传递生成其动态段所需的参数。传递的上下文对象多于路由的动态段:对象。 item.general", 在 transitionToRoute("objects.item.general", id)
  • 考虑迁移到绝对路由路径。以{{link-to 'objects.search.items'}} 为例,将objects.items 资源替换为items 路由。
  • 谢谢!我会试试的

标签: ember.js routes


【解决方案1】:

this.resource() 实际上是一种旧的 Ember 重置命名空间的方法,resetNameSpace: true 属性确实可以做到这一点!

因为您使用了this.resource('objects.items'),您实际上创建了一个重置​​的命名空间,其中objects 是路由。

所以你的新路由器看起来像¹:

this.route('objects', function () {
    this.route('search', { path: '/'});
    this.route('items', { path: '/:search_id'});
    this.route('item', { path: '/item/:item_id' }, function () {
        this.route('general', { path: '/' });
        this.route('tab', { path: '/tab/:tab' });
        this.route('relations');
        this.route('diagram');
        this.route('comments');
        this.route('sources');
        this.route('views');
    });
});

这样你仍然可以使用transitionToRoute("objects.item.general", id)


¹ 注意没有使用resetNameSpace 标志!

【讨论】:

  • 是的,但是 objects.items.item.cmets 也应该是有效的路线。但最终设法用 resetNameSpace 标志解决了这个问题。谢谢!
猜你喜欢
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 2013-01-31
  • 2015-10-15
相关资源
最近更新 更多