我在做流星申请。我在客户端文件夹中的main.js文件中定义了集合变量。在同一位置也有templates.js文件。Template.myTemplate.rendered =函数() {};是在templates.js文件中定义的。我可以访问呈现部分中的集合变量,但它在它之外不可用。
如何使访问“呈现”部分之外的集合变量成为可能?
内部main.js
collectionVariable = new Meteor.Collection('collection_name');内部templates.js
console.log(collectionVariable); // getting "Uncaught ReferenceError: collectionVariable is not defined" here.
Template.myTemplate.rendered = function() {
//Some code. Can access the variable here.
};谢谢
发布于 2013-09-23 10:49:56
请记住文件加载顺序。在您的示例中,templates.js似乎是在main.js之前加载的,因此在计算collectionVariable时没有定义collectionVariable。如果要确保可以在文件正文中使用变量,请使用Meteor.startup
var localVariable;
Meteor.startup(function(){
localVariable = collectionVariable;
console.log(collectionVariable);
});https://stackoverflow.com/questions/18956578
复制相似问题