Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript service.foo不是一个函数_Javascript_Angular_Typescript - Fatal编程技术网

Javascript service.foo不是一个函数

Javascript service.foo不是一个函数,javascript,angular,typescript,Javascript,Angular,Typescript,我的组件无法识别服务中的函数 import { Injectable } from '@angular/core'; import { ToastController } from '@ionic/angular'; @Injectable({ providedIn: 'root' }) export class LocationService { private coordinates: any; constructor( private toastCtrl: Toas

我的组件无法识别服务中的函数

import { Injectable } from '@angular/core';
import { ToastController } from '@ionic/angular';

@Injectable({
  providedIn: 'root'
})
export class LocationService {

  private coordinates: any;
  constructor(
    private toastCtrl: ToastController
  ) { }

  set setCoords(coords: any) {
    this.coordinates = coords;
    this.toastCtrl.create({
      message: `Coordinates Assigned ${this.coordinates.latitude},${this.coordinates.longitude}`
    }).then(alertCtrl => {
      alertCtrl.present();
    });
  }
  get getCoords(): any {
    return this.coordinates;
  }
}

这是我的组件(
app.component.ts
),我正在尝试访问我的功能

  Geolocation.getCurrentPosition({ enableHighAccuracy: true, timeout: 5000 }).then(loc => {
        alert('Latitude: ' + loc.coords.latitude + 'Longitude: ' + loc.coords.longitude);
        console.log('Location: ', loc);
        this.locationService.setCoords(loc);
      }).catch(err => {
        alert(err);
        console.log('Error while getting location:', err);
      });
但不幸的是出现了以下错误,我完全不知道为什么它没有检测到函数,尽管一切似乎都是正确的。我已尝试重新启动应用程序,但无法再次运行

Error while getting location: TypeError: this.locationService.setCoords is not a function
    at app.component.ts:56
    at ZoneDelegate.invoke (zone-evergreen.js:359)
    at Object.onInvoke (core.js:34201)
    at ZoneDelegate.invoke (zone-evergreen.js:358)
    at Zone.run (zone-evergreen.js:124)
    at zone-evergreen.js:855
    at ZoneDelegate.invokeTask (zone-evergreen.js:391)
    at Object.onInvokeTask (core.js:34182)
    at ZoneDelegate.invokeTask (zone-evergreen.js:390)
    at Zone.runTask (zone-evergreen.js:168)

我弄错了,它只是一个访问器,我访问它的方式是错误的,即正确的方式是不带括号访问它。

我弄错了,它只是一个访问器,我访问它的方式是错误的,即正确的方式是不带括号访问它。

错误很容易发现,您使用的是getter和setter:

export class LocationService {

  private coordinates: any;
  constructor(
    private toastCtrl: ToastController
  ) { }

  set setCoords(coords: any) {
    this.coordinates = coords;
    this.toastCtrl.create({
      message: `Coordinates Assigned ${this.coordinates.latitude},${this.coordinates.longitude}`
    }).then(alertCtrl => {
      alertCtrl.present();
    });
  }
  get getCoords(): any {
    return this.coordinates;
  }
}

如果您希望它是一个函数,则应该在函数前面声明它,而不使用
get
set
关键字。错误很容易发现,您使用的是getter和setter:

export class LocationService {

  private coordinates: any;
  constructor(
    private toastCtrl: ToastController
  ) { }

  set setCoords(coords: any) {
    this.coordinates = coords;
    this.toastCtrl.create({
      message: `Coordinates Assigned ${this.coordinates.latitude},${this.coordinates.longitude}`
    }).then(alertCtrl => {
      alertCtrl.present();
    });
  }
  get getCoords(): any {
    return this.coordinates;
  }
}

如果你想让它成为一个函数,你应该在函数前面声明它,而不使用
get
set
关键字

它不是一个函数,你明确地将它设置为访问器。非常感谢,我刚刚回答了我自己的问题。它不是一个函数,你明确地将它设置为访问器。非常感谢,我刚才回答了我自己的问题