我知道这个问题很多次,但我试图找到解决方案,但没有从可用的SO问题中获得。
我是Javascript的新手。我正在尝试使用cordova在android中创建示例计算应用程序。为此,我创建了cordova插件。但我不断得到两个问题。
"Uncaught SyntaxError: Unexpected identifier", source: file:///android_asset/www/js/index.js (36)
这里是index.java代码和错误目标performCalculation()的第一行。
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
document.getElementById("btnCalculate").addEventListener("click", performCalculation);
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
performCalculation: function (){
console.log("in index.html");
var success = function() {
alert("Success");
};
var error = function(message) {
alert("Oopsie! " + message);
};
performAddition(20,10,success,error);
}
};
app.initialize();
这是我得到的第二个例外。
"Uncaught SyntaxError: Unexpected token .", source: file:///android_asset/www/js/calculation.js (3)
这是计算代码.js
var calculationPlugin = {
console.log("calculation");
performAddition: function(first_number, second_number, successCallback, errorCallback) {
console.log("addition");
cordova.exec(
successCallback, // success callback function
errorCallback, // error callback function
'CalculationPlugin', // mapped to our native Java class called "CalculationPlugin"
'addition', // with this action name
[{ // and this array of custom arguments to create our entry
"firstNumber": first_number,
"secondNumber": second_number,
}]
);
}
}
答案 0 :(得分:6)
第一个语法错误
你缺少","在receiveEvent函数之后。
第二语法错误
计算插件是一个对象,因为你有控制台,会抛出错误。从该对象中删除控制台。
答案 1 :(得分:1)
你应该改变这个: app.receivedEvent(' deviceready'); 到 this.receivedEvent(' deviceready');
你只有语法错误,如果你把代码行号放在那里会有所帮助。