【发布时间】:2021-04-26 22:56:39
【问题描述】:
我正在尝试实现一个旧的cordova插件,该插件在ionic中没有任何本机包装器,我可以加载插件和console.log数据,但我无法获取数据以将其推送到全局数组中,我得到了错误“accelerometerData is not defined”,这是我用来观察插件值的函数:
// plugin cordova-plugin-device-motion-fast
declare var navigator: any;
accelerometerData: any[]; //<--- global variable
startAccelerometer(){
var options = { frequency: 75 }; // Update every x seconds
var watchID = navigator.accelerometer.watchAcceleration(
function accelerometerSuccess(acceleration){
let data = {
x: 0,
y: 0,
z: 0,
roll: 0,
pitch: 0,
yaw: 0,
timestamp: 0
}
data.x = acceleration.x;
data.y = acceleration.y;
data.z = acceleration.z;
data.roll = acceleration.roll;
data.pitch = acceleration.pitch;
data.yaw = acceleration.yaw;
data.timestamp = acceleration.timestamp;
console.log(data) //<--- i see the data here
this.accelerometerData.push(data); // <--- null || undefined || error isn't defined
},
function accelerometerError(error){
console.log(error)
}, options);
}
saveTheData(){ <--- save the data to api
const data = {
Accelerometer: this.accelerometerData // <--- null || undefined || error isn't defined
}
this.saveToApi(data).subscribe()....
}
如果我尝试 console.log this.accelerometerData 或尝试在任何其他函数中访问该变量为 null 或未定义...
提前致谢。
【问题讨论】:
-
this指的是 navigator.accelerometer 函数内部的不同范围。这有帮助吗? stackoverflow.com/questions/38818006/…
标签: javascript angular cordova ionic-framework