【问题标题】:I am getting "Type 'string[]' is not assignable to type 'string'" when I have the correct format当我有正确的格式时,我收到“Type 'string []' is notassignable to type 'string'”
【发布时间】:2019-09-10 17:49:27
【问题描述】:

尝试运行以下代码:

      var service = new google.maps.places.PlacesService(this.map);
      service.nearbySearch({
        location: {lat: resp.coords.latitude, lng: resp.coords.longitude},
        radius: 10000,
        type: ['mosque']
      }, (results,status) => {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
          for (var i = 0; i < results.length; i++) {
            this.createMarker(results[i]);
          }
        }
      });

但线路类型:['mosque'] 正在返回

“类型 'string[]' 不可分配给类型 'string'.ts(2322) index.d.ts(2801, 7): 预期的类型来自属性 'type',在此处声明的类型为 'PlaceSearchRequest'"

关于可视化代码,不让我运行这个离子项目。谁能帮我?我检查了 Google Places API 文档,我似乎也有正确的格式和类型。

我在 角度:4.1 离子:3

【问题讨论】:

  • 将字符串 'mosque' 放在括号 [] 中使其成为数组而不是字符串。尝试删除括号,看看是否有帮助。

标签: angular typescript ionic-framework ionic3


【解决方案1】:

所以我解决了上面的问题..

原来上面代码试图运行的PlaceSearchRequest接口来自https://developers.google.com/maps/documentation/javascript/reference/places-service#PlaceSearchRequest

因此,您只需要定义一个字符串而不是列表。我猜以前旧 API 有一个列表作为入口点,因为我正在关注的教程似乎使用它。

所以我的正确代码:

      var service = new google.maps.places.PlacesService(this.map);
      service.nearbySearch({
        location: {lat: resp.coords.latitude, lng: resp.coords.longitude},
        radius: 10000,
        type: ['mosque']
      }, (results,status) => {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
          for (var i = 0; i < results.length; i++) {
            this.createMarker(results[i]);
          }
        }
      });

【讨论】:

  • 代码看起来与您的问题相同?发生了什么变化?你的意思是它应该是type: 'mosque' 而不是type: ['mosque']
猜你喜欢
  • 1970-01-01
  • 2020-01-23
  • 2019-07-01
  • 2022-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-12
  • 2022-09-22
相关资源
最近更新 更多