Ionic 2项目是一个TypeScript项目。
我使用
安装了cordova-background-geolocation-lt
ionic plugin add cordova-background-geolocation-lt
并尝试将其作为旧cordova-plugin-mauron85-background-geolocation
插件的替代品来实现,但我不断收到以下错误。
10 275176 group EXCEPTION: Error: Uncaught (in promise): TypeError: window.BackgroundGeolocation is undefined
插件的配置声明它破坏了window.BackgroundGeolocation。
即使我在app.ts
的构造函数中访问它,我也会收到错误platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
window.BackgroundGeolocation.onMotionchange(
(isMoving) => {
console.log('[js] BackgroundGeoLocation::onMotionChange: ', isMoving);
},
(error) => {
console.log('[js] BackgroundGeoLocation::onMotionChange error: ' + JSON.stringify(error));
}
);
});
离子信息:
Cordova CLI: 6.1.1
Gulp version: CLI version 3.9.1
Gulp local: Local version 3.9.1
Ionic Framework Version: 2.0.0-beta.6
Ionic CLI Version: 2.0.0-beta.25
Ionic App Lib Version: 2.0.0-beta.15
OS:
Node Version: v4.4.3
离子插件列表:
com.transistorsoft.cordova.background-geolocation 1.5.1 "BackgroundGeolocation"
cordova-plugin-console 1.0.3 "Console"
cordova-plugin-dantelo-phonestate 0.0.1 "PhoneState"
cordova-plugin-device 1.1.2 "Device"
cordova-plugin-splashscreen 3.2.2 "Splashscreen"
cordova-plugin-statusbar 2.1.3 "StatusBar"
cordova-plugin-whitelist 1.2.2 "Whitelist"
ionic-plugin-keyboard 2.0.1 "Keyboard"
更新:只需几步即可重现错误。
我用
创建了一个新项目ionic start Test blank --v2 --ts
cd Test
ionic plugin add https://github.com/transistorsoft/cordova-background-geolocation-lt.git
ionic platform add android
最后一行添加了cordova-background-geolocation-lt
所有的依赖项。
然后我通过添加
来修改app.tsdeclare var window:any;
导入后,
window.BackgroundGeolocation.onMotionchange(
(isMoving) => {
console.log('[js] BackgroundGeoLocation::onMotionChange: ', isMoving);
},
(error) => {
console.log('[js] BackgroundGeoLocation::onMotionChange error: ' + JSON.stringify(error));
}
);
到构造函数,如上所示。
运行ionic serve -l -c -s
将因上述undefined
错误而失败。