gpt4 book ai didi

javascript - Ember js - 如何在激活路由时触发 Controller 操作

转载 作者:行者123 更新时间:2023-11-28 18:31:54 25 4
gpt4 key购买 nike

每次 Controller 转换到(“建模器”路线)时,我都需要触发 Controller 操作,但我似乎无法让它工作。我的 router.js 文件是:

var Router = Ember.Router.extend({
location: config.locationType,
actions: {
loading: function() {
this.controllerFor('modeller').send('loading');
},
didTransition: function() {
this.controllerFor('modeller').send('didTransition');
}
}
});

Router.map(function() {
this.route('modeller', { path: 'modeller'});
this.route('welcome', { path: '/'});
});
export default Router;

还有我的建模 Controller :

export default Ember.Controller.extend({
needs: 'modeller',

loading: function () {
console.log("loading")
},

didTransition: function () {
console.log("didTransition")
},
});

但我无法将这些操作中的任何一个记录到控制台。任何指示都非常感激。

更新:

可能应该提到我正在使用 Ember 2.1.0!

所以我使用的解决方案是 setupController - 但我还需要将其放入 app/routes/modeller.js 下的特定建模器路由文件中。以前我试图在 app/router.js 中执行所有操作,但它在那里不起作用。所以最终的解决方案是:

# app/routes/modeller.js
import Ember from 'ember';
export default Ember.Route.extend({

setupController: function(controller, model) {
this.controllerFor('modeller').activate();
}
});

和..

 # app/controllers/modeller.js
export default Ember.Controller.extend({
needs: 'modeller',

activate: function () {
console.log("activate")
},
});

最佳答案

didTransitionRoute 上的方法,而不是 Controller 上的方法,这就是该方法未被调用的原因。

如果您需要从路由影响 Controller ,您需要的钩子(Hook)是 setupController

最佳实践是使用components这将使您能够使用组件 lifecycle hooks .

关于javascript - Ember js - 如何在激活路由时触发 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37823242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com