Template.templatename.onCreated
在文档中提到(以及 .onRendered
和 .onDestroyed
方法)。
但是当我调用这些时,什么也没有发生。如果我打电话 Template.templatename.created
例如,这有效。
知道发生了什么吗?我是否误读了文档中的某些内容?这是对别的东西的引用吗?
编辑:我刚刚在来源中找到了这个:
https://github.com/meteor/meteor/blob/master/packages/blaze/template.js#L65
在第 180 行,这些在 1.1 中被标记为已弃用,但我仍然没有从 onCreated 得到任何爱......
有谁知道我做错了什么?
Template.channels_admin.onCreated = function () {
// .... doesn't run
};
最佳答案
在meteor 1.0.4之前,created
是一个可用于在任何模板逻辑(事件、渲染、帮助程序等)之前运行的所有模板的函数。在 1.0.4 中,它被替换为 onCreated这是一个函数 注册回调 (同样,每个回调在任何模板逻辑之前运行一次)。created
留在原地以免破坏现有代码,但被认为已弃用 onCreated
.
因为 created
是模板上的一个函数,它可以通过以下方式分配:
Template.myTemplate.created = function() {console.log('here');};
created
不应直接调用。正如您从文档中看到的,
onCreated
用这样的函数调用:Template.myTemplate.onCreated(function() {
console.log('hello');
});
关于
onCreated
的好处是你可以为同一个模板注册多个回调。在实践中,这可能不会经常出现,但是在您需要附加到外部包提供的模板的情况下,这很好。同样的逻辑适用于 onRendered
和 onDestroyed
.
关于javascript - Meteor 中的 Template.created 和 Template.onCreated 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30417066/