钛加速器; CommonJS没有方法
我是Appcelerator的新手,我试图导入自己的commonJS库。我也跟着上钛加速器; CommonJS没有方法
http://docs.appcelerator.com/titanium/latest/#!/guide/CommonJS_Modules_in_Titanium
的指示,创建了一个名为“logger.js”一个新的文件,用下面的代码:
exports.info = function(str) {
Titanium.API.info(new Date()+': '+str);
};
现在我只是努力让与exceute此代码:
var logger = require('logger');
logger.info('TEST TEST TEST');
就像在这个例子中一样。他找到了该文件,但不承认我的方法,我得到以下异常:
[ERROR] : TiExceptionHandler: (main) [602,602] ----- Titanium Javascript Runtime Error -----
[ERROR] : TiExceptionHandler: (main) [0,602] - In alloy/controllers/index.js:100,12
[ERROR] : TiExceptionHandler: (main) [0,602] - Message: Uncaught TypeError: Object function Controller() {
[ERROR] : TiExceptionHandler: function logOutput(str) {
[ERROR] : TiExceptionHandler: Titanium.API.info(str);
[ERROR] : TiExceptionHandler: }
[ERROR] : TiExceptionHandler: require("alloy/controllers/BaseController").apply(this, Array.prototype.slice.call(arguments));
[ERROR] : TiExceptionHandler: this.__controllerPath = "login";
[ERROR] : TiExceptionHandler: if (arguments[0]) {
[ERROR] : TiExceptionHandler: __processArg(arguments[0], "__parentSymbol");
[ERROR] : TiExceptionHandler: __processArg(arguments[0], "$model");
[ERROR] : TiExceptionHandler: __processArg(arguments[0], "__itemTemplate");
[ERROR] : TiExceptionHandler: }
[ERROR] : TiExceptionHandler: var $ = this;
[ERROR] : TiExceptionHandler: var exports = {};
[ERROR] : TiExceptionHandler: exports.destroy = function() {};
[ERROR] : TiExceptionHandler: _.extend($, $.__views);
[ERROR] : TiExceptionHandler: exports = logOutput;
[ERROR] : TiExceptionHandler: _.extend($, exports);
[ERROR] : TiExceptionHandler: } has no method 'info'
[ERROR] : TiExceptionHandler: (main) [1,603] - Source: logger.info("TEST TEST TEST");
[ERROR] : V8Exception: Exception occurred at alloy/controllers/index.js:100: Uncaught TypeError: Object function Controller() {
[ERROR] : V8Exception: function logOutput(str) {
[ERROR] : V8Exception: Titanium.API.info(str);
[ERROR] : V8Exception: }
[ERROR] : V8Exception: require("alloy/controllers/BaseController").apply(this, Array.prototype.slice.call(arguments));
[ERROR] : V8Exception: this.__controllerPath = "login";
[ERROR] : V8Exception: if (arguments[0]) {
[ERROR] : V8Exception: __processArg(arguments[0], "__parentSymbol");
[ERROR] : V8Exception: __processArg(arguments[0], "$model");
[ERROR] : V8Exception: __processArg(arguments[0], "__itemTemplate");
[ERROR] : V8Exception: }
[ERROR] : V8Exception: var $ = this;
[ERROR] : V8Exception: var exports = {};
[ERROR] : V8Exception: exports.destroy = function() {};
[ERROR] : V8Exception: _.extend($, $.__views);
[ERROR] : V8Exception: exports = logOutput;
[ERROR] : V8Exception: _.extend($, exports);
[ERROR] : V8Exception: } has no method 'info'
我想它是如此简单,但我不知道哪里是我的错。
在此先感谢
最后我知道了。问题是我正在使用“合金项目”,就像“Alejandro F. Carrera”提到的那样。我只需要使用Alloy.createController();
就可以工作。
var logger = Alloy.createController('logger');
logger.info('TEST TEST TEST');
现在它工作。
感谢大家指点我正确的方向
您显示的代码适用于我。您是否在app/lib目录中创建了logger.js?
也许你应该尝试注释掉index.js的logger.info(...)行只是为了确保你正在寻找正确的问题;-)
哪个版本的钛工作室是你使用? - 以及在哪个操作系统上?
/约翰
最好是出口的主要对象和访问信息功能(钛良好做法)。
logger.js
你需要记录var loggerObject = require('logger.js'); // (both files are at the same Path)
loggerObject.info("TEST TEST");
我希望我的回答可以帮助你
var logger = (function(){
var self = {};
self.info = function info(str)
{
Ti.API.info(new Date()+': '+str);
};
return self;
}());
module.exports = logger;
file.js;)
嗨,可惜我得到了同样的例外:-(。 – Murolack 2014-08-28 06:15:38
您的项目是合金类型不移动项目,所以你必须阅读此链接[钛文档](http://docs.appcelerator.com/titanium/latest/#!/ guide/Alloy_Controllers-section-34636384_AlloyControllers-LibraryCodeandCommonJSModules) – 2014-08-28 07:32:07
通常我们习惯于把这种类型的附加功能lib目录下的文件,所以你应该创建一个文件夹,并在应用程序目录下将它命名为lib,并将logger.js文件放在该文件夹下,然后重试。
我也是这样做的,但不幸的是得到了相同的结果:-( – Murolack 2014-08-28 06:46:29
嗨,该文件位于应用程序/控制器下。如果我取消注释行logger.info(...)我没有得到任何异常。 – Murolack 2014-08-28 05:58:16
我正在使用Windows 7 Titanium Studio 3.3.0和Titanium SDK 3.3.0.GA.我添加了这行'Ti.API.log(“Logger:”+ logger);'我可以看到他找到我的文件,甚至输出我的方法'... e.info = function(t){Titanium.API。 info(new Date +“:”+ t)} ...'。 – Murolack 2014-08-28 06:07:56
好的,第一件事就是,据我了解,这些类型的CommonJS库应该放在app/lib下。但是,这似乎不是你的问题 - 只是一个更好的做法;-) – 2014-08-28 08:00:10