ember 路由将动态 url 传递给子路由

ember route pass dynamic url to child route

如果我在下面定义了路线:

App.Router.map(function () {
    this.resource('user', {path: "/:user"}, function () {
        this.route('work', {path: "/work"});
    })
});

我可以在用户控制器中获取 :user 值,但是如何在我的工作控制器或工作路由中获取该值?

你可以在路由中使用modelFor并在model(或beforeModelafterModel)钩子中设置user控制器属性: http://emberjs.com/api/classes/Ember.Route.html#method_modelFor

//work route
model: function() {
  return Ember.RSVP.hash({
    user: this.modelFor('user'), // here user is routeName
    work: // your logic here
  });
},

setupController: function(controller, models) {
  controller.setProperties(models);
}

您可以在 setupController 挂钩的工作路径中在控制器上设置它:

setupController(controller) {
  controller.set('userId', this.modelFor('user').get('id'));
  return this._super(...arguments);
}
// ES < 6 syntax
setupController: function(controller) {
  controller.set('userId', this.modelFor('user').get('id'));
  return this._super.apply(this, arguments);
}