【问题标题】:How to use typescript class method inside Angular app component如何在 Angular 应用程序组件中使用 typescript 类方法
【发布时间】:2018-03-08 04:55:08
【问题描述】:

我对 Angular 5 和打字稿完全陌生。我想在我的 Angular 组件中使用这个 JS 代码(http://jsbin.com/rorecuce/1/edit?html,output)所以,我尝试将它转换成这样的打字稿类(对不起,我不知道它是否正确,如果有人请检查并纠正我)

    class MercatorProjection {
  TILE_SIZE = 256;
  pixelOrigin_ : any;
  pixelsPerLonDegree_: any;
  pixelsPerLonRadian_: any;

  bound(value, opt_min, opt_max) {
    if (opt_min !== null) value = Math.max(value, opt_min);
    if (opt_max !== null) value = Math.min(value, opt_max);
    return value;
  }

  degreesToRadians(deg) {
    return deg * (Math.PI / 180);
  }

  radiansToDegrees(rad) {
    return rad / (Math.PI / 180);
  }

  MercatorProjection() {
    let pixelOrigin_ = new google.maps.Point(this.TILE_SIZE / 2, this.TILE_SIZE / 2);
    var pixelsPerLonDegree_ = this.TILE_SIZE / 360;
    var pixelsPerLonRadian_ = this.TILE_SIZE / (2 * Math.PI);
  }

  public fromLatLngToPoint(latLng,
    opt_point) {

      var point = opt_point || new google.maps.Point(0, 0);
      var origin = this.pixelOrigin_;

      point.x = origin.x + latLng.lng() * this.pixelsPerLonDegree_;

      var siny = this.bound(Math.sin(this.degreesToRadians(latLng.lat())), - 0.9999,
      0.9999);
      point.y = origin.y + 0.5 * Math.log((1 + siny) / (1 - siny)) * - this.pixelsPerLonRadian_;
      return point;
  }

  public fromPointToLatLng(point) {

    var origin = this.pixelOrigin_;
    var lng = (point.x - origin.x) / this.pixelsPerLonDegree_;
    var latRadians = (point.y - origin.y) / -this.pixelsPerLonRadian_;
    var lat = this.radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2);
    return new google.maps.LatLng(lat, lng);
  }
}

在我的 app.component.ts 中,我有一个 getNewRadius() 方法,我想访问上述方法。这是我尝试过的。

getNewRadius(){
  var numTiles = 1 << this.map.getZoom()
  var center =this.map.getCenter();
  var moved = google.maps.geometry.spherical.computeOffset(center, 10000, 90); /*1000 meters to the right*/
  var projection = new MercatorProjection;
  var initCoord = MercatorProjection.fromLatLngToPoint(center);
  var endCoord = MercatorProjection.fromLatLngToPoint(moved);
  var initPoint = new google.maps.Point(
    initCoord.x * numTiles,
    initCoord.y * numTiles);
   var endPoint = new google.maps.Point(
    endCoord.x * numTiles,
    endCoord.y * numTiles);
  var pixelsPerMeter = (Math.abs(initPoint.x-endPoint.x))/10000.0;
  var totalPixelSize = Math.floor(this.desiredRadiusPerPointInMeters*pixelsPerMeter);
  console.log(totalPixelSize);
  return totalPixelSize;

}

但我收到错误 [ts] 类型“typeof MercatorProjection”上不存在属性“fromLatLngToPoint”。

【问题讨论】:

  • @messerbill - 请检查一下

标签: angular typescript


【解决方案1】:

您的方法需要 2 个参数

 public fromLatLngToPoint(latLng, opt_point) ...

而你只用一个来调用它

var initCoord = MercatorProjection.fromLatLngToPoint(center);
var endCoord = MercatorProjection.fromLatLngToPoint(moved);

试试这个

用 2 个参数调用它

var emptyPoint = new google.maps.Point(0, 0);
var initCoord = MercatorProjection.fromLatLngToPoint(center, emptyPoint);
var endCoord = MercatorProjection.fromLatLngToPoint(moved, emptyPoint);

并在从类中调用方法时声明您的方法为静态

public static fromLatLngToPoint(latLng,opt_point) { ...

【讨论】:

  • 这不是问题。请检查 jsbin.com/rorecuce/1/edit?html,在这个中输出相同
  • 我在这个链接中没有看到任何错误......你在谈论打字稿,这个链接只是 js,你从哪里得到错误?
  • 在那个链接中,它运行良好。是的,这是 JS,我需要将其转换为 typescript。
  • Typescript 是“Typed”javascript,所以如果你调用一个接受 2 个参数的方法,你必须用 2 个参数调用它,否则它会给你这个错误,因为它找不到它。实现相同的方法,但只使用一个参数,并使用其中的 2 个参数调用该方法。
  • 编辑了我的答案
猜你喜欢
  • 2016-05-12
  • 2020-01-11
  • 2016-03-01
  • 1970-01-01
  • 2017-02-13
  • 2016-05-17
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多