[英]Pebble JavaScript Multiple JS Files (Pebble.js)
I'm creating a project on CloudPebble using JavaScript. 我正在使用JavaScript在CloudPebble上创建一个项目。
I have a "Constants.js" which hosts a variable that I would like to access using "app.js", which is the main contents of the app. 我有一个“ Constants.js”,其中包含一个我想使用“ app.js”访问的变量,这是应用程序的主要内容。 However, running the app I receive the following error: 但是,运行该应用程序时,出现以下错误:
[PHONE] pebble-app.js:?: JavaScript Error:
TypeError: Cannot read property 'length' of undefined
Here is my code: 这是我的代码:
Constants.js Constants.js
var mainMenuOptions = ["MenuOption1", "MenuOption2", "MenuOption3"];
app.js app.js
var UI = require('ui');
var Vector2 = require('vector2');
var constants = require('Constants.js');
var mainMenu = new UI.Menu({
});
for (var i = 0; i < constants.mainMenuOptions.length; i++) { //Error occurs here
mainMenu.item(0, i, { title: constants.mainMenuOptions[i] });
}
...
Any help is appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!
I beleive your Constants.js
should have this format: 我相信您的Constants.js
应该具有以下格式:
var Constants = {
mainMenuOptions: ["MenuOption1", "MenuOption2", "MenuOption3"]
};
this.exports = Constants;
And then in app.js
do 然后在app.js
执行
var constants = require('Constants');
to access it. 访问它。
Used this approach in my very first Pebble.js app Autoinsult and it worked. 用这种方法在我的第一个Pebble.js应用Autoinsult和它的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.