【问题标题】:MeteorJS - variable not definedMeteorJS - 未定义变量
【发布时间】:2013-08-26 21:01:28
【问题描述】:

我是流星新手。我刚刚使用流星创建了一个 Hello world 项目。目前我的项目结构很简单。

  • 根文件夹
    • abc.css
    • abc.html
    • abc.js

在 abc.js 中,我只是尝试声明一个像这样的变量:

var lists = new Meteor.Collection("Lists");

if (Meteor.isClient) {
  Template.hello.greeting = function () {
  return "My List.";
  };

  Template.hello.events({
   'click input' : function () {
   if (typeof console !== 'undefined')
      console.log("You pressed the button");
   }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
  });
}

但是当我运行它时,我在浏览器控制台中收到以下错误:

[18:17:32.895] ReferenceError: 列表未定义

我不确定我做错了什么。

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    在 Meteor 中,变量的作用域是一个文件。因此,如果您使用 var 关键字定义列表,您将无法在 abc.js 之外访问 lists

    要通过这个,只需删除 var 就可以了:

    lists = new Meteor.Collection("Lists");
    

    然后您可以在其他文件以及您的控制台中访问它。

    【讨论】:

      猜你喜欢
      • 2013-12-01
      • 2011-06-11
      • 2021-02-27
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多