首页 > 解决方案 > Ember JS - 从资源路由到路由

问题描述

我正在尝试将项目从 2.x 版更新和 ember 到 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 吗?

标签: ember.jsroutes

解决方案


由于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使用标志!


推荐阅读