angular - 我无法将我的类(class)设置为 Ionic2 上的 promise

标签 angular typescript ionic2

当我执行函数 getLocalPosition() 并在内部尝试设置类 Position 的变量时,我收到下一个错误。

TypeError: Cannot read property 'setLat' of null
    at VM11086 main.js:52673
    at t.invoke (VM11035 polyfills.js:3)
    at Object.onInvoke (VM11086 main.js:34675)
    at t.invoke (VM11035 polyfills.js:3)
    at e.run (VM11035 polyfills.js:3)
    at VM11035 polyfills.js:3
    at t.invokeTask (VM11035 polyfills.js:3)
    at Object.onInvokeTask (VM11086 main.js:34666)
    at t.invokeTask (VM11035 polyfills.js:3)
    at e.runTask (VM11035 polyfills.js:3)
VM11086 main.js:52685 TypeError: Cannot read property 'setLon' of null
    at VM11086 main.js:52681
    at t.invoke (VM11035 polyfills.js:3)
    at Object.onInvoke (VM11086 main.js:34675)
    at t.invoke (VM11035 polyfills.js:3)
    at e.run (VM11035 polyfills.js:3)
    at VM11035 polyfills.js:3
    at t.invokeTask (VM11035 polyfills.js:3)
    at Object.onInvokeTask (VM11086 main.js:34666)
    at t.invokeTask (VM11035 polyfills.js:3)
    at e.runTask (VM11035 polyfills.js:3)

但是如果我在构造函数中设置我的类就可以正常工作。

这是我的.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Geolocation } from 'ionic-native';
import { Storage } from '@ionic/storage';
import { Position } from './clases';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  private position = new Position();

  constructor(public navCtrl: NavController, public storage: Storage) {
    this.position.setLat(1234311);
    console.log(this.position);
    // Or to get a key/value pair
    this.storage.get('position').then((val) => {
      this.position = val;
    }).catch((err) => {
      console.log(err);
    });
  }
  getLocalPosition() {
    Geolocation.getCurrentPosition().then((resp => {

      try {
        this.position.setLat(resp.coords.latitude);
      } catch (e) {
        if (e instanceof TypeError) {
          console.log(e);

        }
      }
      try {
        this.position.setLon(resp.coords.longitude);
      } catch (e) {
        if (e instanceof TypeError) {
          console.log(e);

        }
      }

      //this.storage.set('position', this.position);
    }))
  }


}

这是我的课

export class Position {
    private lat: number;
    private lon: number;

    constructor(lat: number = 0, lon: number = 0) {
        this.lat = lat;
        this.lon = lon;

    }
    setLat(lat: number) {
        this.lat = lat;
    }
    setLon(lon: number) {
        this.lon = lon;
    }
    getLat() {
        return this.lat;
    }
    getLon() {
        return this.lon;
    }

    getPosition() {
        let position = {
            lat: this.lat,
            lon: this.lon
        }
        return position;
    }
}

谢谢你的时间

最佳答案

问题出在这一行

this.storage.get('position').then((val) => {
  this.position = val;
}).catch((err) => {
  console.log(err);
});

我用 val 设置了 this.position,之后无法访问方法 setLat,因为它不存在。

关于angular - 我无法将我的类(class)设置为 Ionic2 上的 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41465938/

相关文章:

javascript - TypeScript 中的对象类型转换

css - 如何使用ion-row和ion-col对齐ionic 2中的元素?

javascript - 如何摆脱具有早期返回的多个嵌套 switchMap

javascript - 如何获取所需格式的日期 : ionic 4

angular - 如何在表单数组中添加无效的表单控件而不影响其功能

html - (Ionic 2) 选择框内的分组标签

typescript - Angular 2 : Inject external Component into other component via directive not working

angular 4 - 从父组件 html 调用子组件函数

javascript - 简单的 react 计数器

Angular 结合 2 个 promise 到 1 个 promise