我正在尝试在我的新项目中实现地理定位。我已经安装了以下插件并在app.module.ts中添加了它们
ionic cordova plugin add cordova-plugin-mauron85-background-geolocation npm install --save @ionic-native/background-geolocation
我正在跟踪本教程,但在home.ts中出现了错误。下面是我的home.ts代码。
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(private backgroundGeolocation: BackgroundGeolocation,public navCtrl: NavController) {
}
const config: BackgroundGeolocationConfig = {
desiredAccuracy: 10,
stationaryRadius: 20,
distanceFilter: 30,
debug: true, // enable this hear sounds for background-geolocation life-cycle.
stopOnTerminate: false, // enable this to clear background location settings when the app terminates
};
this.backgroundGeolocation.configure(config)
.subscribe((location: BackgroundGeolocationResponse) => {
console.log(location);
// IMPORTANT: You must execute the finish method here to inform the native plugin that you're finished,
// and the background-task may be completed. You must do this regardless if your HTTP request is successful or not.
// IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
//this.backgroundGeolocation.finish(); // FOR IOS ONLY
});
// start recording location
this.backgroundGeolocation.start();
// If you wish to turn OFF background-tracking, call the #stop method.
this.backgroundGeolocation.stop();
}错误出现在这一行:this.backgroundGeolocation.configure(config)。在this上有这样的说法;
意想不到的记号。需要构造函数、方法、访问器或属性。
在配置上,它说:
ts找不到名称“config”
发布于 2017-11-26 17:24:21
正如@SurajRao在他的评论中指出的那样,您需要将代码移动到类构造函数中或将其包装在方法中。
下面是放置在构造函数中的代码的组件:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
constructor(private backgroundGeolocation: BackgroundGeolocation,public navCtrl: NavController) {
const config: BackgroundGeolocationConfig = {
desiredAccuracy: 10,
stationaryRadius: 20,
distanceFilter: 30,
debug: true,
stopOnTerminate: false,
};
this.backgroundGeolocation.configure(config)
.subscribe((location: BackgroundGeolocationResponse) => {
console.log(location);
});
this.backgroundGeolocation.start();
this.backgroundGeolocation.stop();
}
}阅读更多关于MDN上的JavaScript类的信息。
https://stackoverflow.com/questions/47495673
复制相似问题