更改查询参数不刷新路由

Change in Query Param not refreshing the route

我已经在路由中设置了 queryParam 设置,以便在查询参数更改时刷新模型。但是,当查询参数更改时 setupController 不会被调用。知道为什么会这样吗?

export default Route.extend({
  queryParams: {
    id: {
      refreshModel: true
    }
  },

  setupController(controller) {
    controller.start(); // fetches data synchronously from api
  }
});

// 解决感谢alptugd的回复

export default Route.extend({
  queryParams: {
    id: {
      refreshModel: true
    }
  },

  model() {
    // Returning a new array/object/value everytime the hook is called, causes the 
    // setupController hook to be called.
    return [];
  }

  setupController(controller) {
    controller.start(); // fetches data synchronously from api
  }
});

refreshModel 强制路由到 refresh 并且正如预期的那样,您应该期望 beforeModelmodelafterModelsetupController 挂钩到 运行。然而; setupController hook:

有细微差别

查看 source code or relevant API documentation 中 Ember 的 route.js 方法的 refresh 方法的注释。都说:

"Refresh the model on this route and any child routes, firing the beforeModel, model, and afterModel hooks in a similar fashion to how routes are entered when transitioning in from other route. The current route params (e.g. article_id) will be passed in to the respective model hooks, and if a different model is returned, setupController and associated route hooks will re-fire as well."

这意味着;为了在 refresh 发生时触发 setupControllermodel 钩子应该返回一个不同的模型。在你的情况下;您没有任何型号hook;因此 setupController 不会在 id 参数值更改时被调用。

顺便说一句;如果您覆盖 setupController 挂钩,调用超级方法是一个好习惯,因为它的唯一目的是将 model 属性 保存到 controller。请查看 API 以获得有关此案例的详细说明。