流星:全局常量未从 app/lib/_constants.js 中获取
Meteor: Global constant not getting picked up from app/lib/_constants.js
我的应用程序目录结构是:
App
├── client
├── lib
│ ├── _constants.js
│ ├── config
│ └── router
├── modules
│ ├── answers
│ └── questions
├── node_modules
│ └── bcrypt
├── public
│ └── imgs
├── server
│ ├── lib
│ ├── roles
│ └── startup
└── settings-example.json
在我的 _constants.js
中,我定义了一些全局变量,例如Schemas = {}
我打算在 modules > module_name> lib > collections.js
或 modules > module_name> lib > methods.js
中使用
但是在模块 collections.js 中找不到全局变量。这是我得到的错误:
W20160323-21:38:58.977(-7)? (STDERR) ReferenceError: Schemas is not defined
W20160323-21:38:58.977(-7)? (STDERR) at modules/answers/lib/collections.js:22:1
W20160323-21:38:58.977(-7)? (STDERR) at modules/answers/lib/collections.js:89:1
根据我的理解,APP/lib/_constants.js
文件中的全局变量应该在更深的modules/module_name/lib/collections.js
加载之前加载,对吧?
但这显然不会发生。我究竟做错了什么?
感谢您的帮助!
阅读 Structuring your application 中的 "file load order" 部分:
There are several load ordering rules. They are applied sequentially to all applicable files in the application, in the priority given below:
- HTML template files are always loaded before everything else
- Files beginning with main. are loaded last
- Files inside any lib/ directory are loaded next
- Files with deeper paths are loaded next
- Files are then loaded in alphabetical order of the entire path
这是实现的方式,在 之前 加载嵌套较浅的 lib
,这解释了您的问题。以下是一些选项:
- 不要在你的深度路径中使用
lib
。例如。将路径重命名为 modules/questions/stuff/collections.js
.
- 将您的模块移动到包中。
- 升级到 meteor 1.3(在撰写本文时仍处于预发布状态)并开始使用显式
export
/import
模块语法。
我的应用程序目录结构是:
App
├── client
├── lib
│ ├── _constants.js
│ ├── config
│ └── router
├── modules
│ ├── answers
│ └── questions
├── node_modules
│ └── bcrypt
├── public
│ └── imgs
├── server
│ ├── lib
│ ├── roles
│ └── startup
└── settings-example.json
在我的 _constants.js
中,我定义了一些全局变量,例如Schemas = {}
我打算在 modules > module_name> lib > collections.js
或 modules > module_name> lib > methods.js
但是在模块 collections.js 中找不到全局变量。这是我得到的错误:
W20160323-21:38:58.977(-7)? (STDERR) ReferenceError: Schemas is not defined
W20160323-21:38:58.977(-7)? (STDERR) at modules/answers/lib/collections.js:22:1
W20160323-21:38:58.977(-7)? (STDERR) at modules/answers/lib/collections.js:89:1
根据我的理解,APP/lib/_constants.js
文件中的全局变量应该在更深的modules/module_name/lib/collections.js
加载之前加载,对吧?
但这显然不会发生。我究竟做错了什么?
感谢您的帮助!
阅读 Structuring your application 中的 "file load order" 部分:
There are several load ordering rules. They are applied sequentially to all applicable files in the application, in the priority given below:
- HTML template files are always loaded before everything else
- Files beginning with main. are loaded last
- Files inside any lib/ directory are loaded next
- Files with deeper paths are loaded next
- Files are then loaded in alphabetical order of the entire path
这是实现的方式,在 之前 加载嵌套较浅的 lib
,这解释了您的问题。以下是一些选项:
- 不要在你的深度路径中使用
lib
。例如。将路径重命名为modules/questions/stuff/collections.js
. - 将您的模块移动到包中。
- 升级到 meteor 1.3(在撰写本文时仍处于预发布状态)并开始使用显式
export
/import
模块语法。