我有一个用于更新特定项目数据的 Iron-router 路由:
Router.route('/project/:key/update', {
...
});
每次用户导航到“编辑项目页面”时,我都希望关注项目名称输入。
template.onRendered(function() {
this.$('form input[name="project_name"]').focus();
});
从仪表板导航到任何给定的编辑项目页面时,这非常有效。但是,从一个项目页面导航到另一个项目页面
onRendered
函数不会重新运行,因此输入没有聚焦。
最佳答案
您可以强制onRendered通过添加对 currentData 的检查在上下文更改后重新评估内部 autorun像这样:
Template.myTemplate.onRendered(function() {
var self = this;
this.autorun(function() {
// hack to force the autorun to reevaluate
Template.currentData();
// insert onRendered code here
self.$('form input[name="project_name"]').focus();
});
});
详情见this issue文末.
关于meteor - 当路由更改但模板保持不变时,如何让 onRendered 再次运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29907410/