Javascript 如何将js文件添加到Tianium mobile项目
我在一个简单的Tianium Mobile项目中不断遇到以下错误:Javascript 如何将js文件添加到Tianium mobile项目,javascript,android,titanium-mobile,Javascript,Android,Titanium Mobile,我在一个简单的Tianium Mobile项目中不断遇到以下错误: Location: app.js Message: Uncaught ReferenceError: tab2 is not defined Source: tabGroup.addTab(tab2); 以下是我的app.js文件中的代码: // create tab group var tabGroup = Titanium.UI.createTabGroup(); // create the window var
Location:
app.js
Message:
Uncaught ReferenceError: tab2 is not defined
Source: tabGroup.addTab(tab2);
以下是我的app.js文件中的代码:
// create tab group
var tabGroup = Titanium.UI.createTabGroup();
// create the window
var win1 = Ti.UI.createWindow({
width: 320,
height: 440,
top: 0,
left: 0,
backgroundImage: 'background.png',
title: 'loan calculator',
barImage: 'navbar.png'
});
// creat the view, this will hold all of our UI controls
// note the hight of this view is the height of the window
// minus 134px for the status bar and padding and adjusted for navbar
var view = Ti.UI.createView({
width: 300,
height: win1.height - 134,
left: 10,
top: 10,
backgroundColor: '#fff',
borderRadius: 5
});
// we will give the logo a left margin so it centers neatly
// within our view
var _logoMarginLeft = (view.width - 253) / 2;
// now let's add our logo to an imageview and add that to our
// view object
var logo = Ti.UI.createImageView({
backgroundImage: 'logo.png',
width: 253,
height: 96,
left: _logoMarginLeft,
top: 0
});
view.add(logo);
// add the view to our window
win1.add(view);
// add the first tab and attach our window object (win1) to it
var tab1 = Ti.UI.createTab({
icon: 'icon_calculator.png',
title: 'Calculate',
window: win1
});
// create the second window for settings tab
var win2 = Ti.UI.createWindow({
width: 320,
height: 440,
top: 0,
left: 0,
backgroundImage: 'background.png',
url: 'window2.js',
title: 'Settings',
barImage: 'navbar.png'
});
// now add the tabs to our tabGroup object
tabGroup.addTab(tab1);
tabGroup.addTab(tab2);
// open the tabgroup to launch the app
tabGroup.open();
以下是我的window2.js中的代码:
// add the second tab and attach our external window object
// (win2 / window2.js) to it
var tab2 = Ti.UI.createTab({
icon: 'icon_settings.png',
title: 'Settings',
window: win2
});
如何解决这个问题?为什么要将tab2创建从app.js移到window2.js?你想通过这个改变实现什么 Tab1构造正确。。。tabGroup包含作为窗口(window1)容器创建的选项卡(tab1)。第二个选项卡的创建顺序错误 另外,当您使用createWindow的url表单时,它会创建一个全新的上下文。该窗口中的项无法访问父范围,反之亦然
最后,作为额外的好处,app.js可能会在window2.js执行之前完成。URL加载是异步的,创建上下文需要时间,因此即使可以跨上下文访问,tab2很可能在添加到选项卡组时还没有创建。我对这些计时问题有很多“乐趣”…我在一本Appcelerator钛合金智能手机应用程序开发食谱中测试代码。我已经在一个单独的文件中编程了windows,但从来没有一个选项卡,所以我只想正确地完成它。