繁体   English   中英

使用CoffeeScript时无法在客户端访问Meteor Collection

[英]Meteor Collection is not accessible in client side when using CoffeeScript

如果我使用简单的.js文件并在其中创建一个集合

Posts = new Meteor.Collection("posts");
if(Meteor.isClient){
...
...
}

我可以在浏览器中访问“帖子”集合,即

> Posts.find().count();
  4

但是如果我使用CoffeScript而不是JavaScript

Posts = new Meteor.Collection "posts"
if Meteor.isClient
 ...
 ...

我无法访问,它引发错误

> Posts.find().count();
  ReferenceError: Posts is not defined

因为CoffeeScript变量默认情况下是文件作用域的。我如何解决此问题。

因为CoffeeScript变量默认情况下是文件作用域的。我如何解决此问题。

@前缀,它将编译this. 在JavaScript中,因此它将被添加到window对象,该对象是JavaScript客户端的全局范围。

@Posts = new Meteor.Collection("posts");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM