修复TS2345:无法将类型“ HTMLElement”的参数分配给类型“ HTMLInputElement”的参数

卢西安·杜波依斯(Lucien Dubois)

我正在尝试在新的Ionic应用程序中设置Google Maps Places Autocomplete

这是问题所在。在第一次搜索中,我在控制台中收到此错误:

TypeError: Cannot read property 'place_id' of undefined

和终端中的此错误:

TS2345: Argument of type 'HTMLElement' is not assignable to parameter of type 'HTMLInputElement'

但是,在第二次搜索中,我得到了place_id而没有任何错误。

这是我的(简化).ts文件

import { Component, OnInit } from '@angular/core';
import { google } from "google-maps";
import { Platform } from '@ionic/angular';

@Component({...})
export class AddaddressPage implements OnInit {

  autocomplete:any;

  constructor(public platform: Platform) {}

    ngOnInit() {
        this.platform.ready().then(() => {
            this.autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'));
            this.autocomplete.setFields(['place_id']);
        });
    }

    fillInAddress() {
      var place = this.autocomplete.getPlace();
      console.log(place);
      console.log(place.place_id);
    }

}

和我使用的输入:

<input id="autocomplete" type="text" (change)="fillInAddress()" />

我应该如何进行?

卢西安·杜波依斯(Lucien Dubois)

玩了之后,这是把戏!需要ViewChild和离子输入。

.html

<ion-input #autocomplete type="text"></ion-input>

.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { google } from "google-maps";
import { Platform } from '@ionic/angular';

@Component(...)
export class AddaddressPage implements OnInit {

  googleAutocomplete:any;

  @ViewChild('autocomplete') autocompleteInput: ElementRef;

  constructor(public platform: Platform) {  }

    ngOnInit() {
        this.platform.ready().then(() => {
            this.autocompleteInput.getInputElement().then((el)=>{
                this.googleAutocomplete = new google.maps.places.Autocomplete(el);
                this.googleAutocomplete.setFields(['place_id']);
                this.googleAutocomplete.addListener('place_changed', () => {
                    var place = this.googleAutocomplete.getPlace();
                    console.log(place);
                    console.log(place.place_id);
                });
            })

        });
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将代码重构为单独的文件时,“ TS2345:类型的参数无法分配给类型的参数”

错误TS2345:“菜单”类型的参数无法分配给类型的参数

“ HTMLElement”类型的参数不能分配给“ CanvasImageSource”类型的参数

打字稿错误TS2345错误:TS2345:“缓冲区”类型的参数无法分配给“字符串”类型的参数

错误TS2345:类型'X'的参数不能分配给类型'X []'的参数

错误TS2345:类型“ T”的参数不能分配给类型“对象”的参数

Person 类型的 TypeScript 错误 (TS2345) 参数 | undefined 不能分配给 Person 类型的参数

TS2345:“RolesGuard”类型的参数不可分配给“CanActivate”类型的参数

类型'HTMLElement |的参数 “ null”不可分配给“ Element”类型的参数。类型'null'不能分配给类型'Element'.ts(2345)

打字稿错误“无法将类型参数分配给类型参数”

使用Jest无法将类型“ openURL”的参数分配给类型“从不”的参数

无法将类型“未知”的参数分配给类型“字符串”的参数

无法将类型'MemoizedSelector <ICommonAppState,IMenuItemsObject []>'的参数分配给类型'string'的参数

无法将类型“ ApolloServer”的参数分配给类型“ ApolloServerBase”的参数

无法将类型“未知”的参数分配给类型“ {}”的参数

TS2345:“数字|数字”类型的参数 未定义”不能分配给“数字”类型的参数

错误TS2345:类型'OperatorFunction <User,void>'的参数不能分配给类型'OperatorFunction <Object,void>'的参数

Angular 5 错误 TS2345:“数字”类型的参数不可分配给“字符串”类型的参数

Angular - 错误 TS2345:“NgForm”类型的参数不可分配给“Ussdthirdpartyapp”类型的参数

无法将类型为'HttpEvent <any>'的角形参数分配给该参数

无法在Visual Studio中将类型为'string'的参数分配给类型为'number'的参数

TS - '(x: HTMLElement) => void' 类型的参数不可分配给参数

错误TS2345:类型'(response:string)=> void'的参数不能分配给类型'(((value:{})=> void | PromiseLike <void>)| 空值

Flutter - 不能将函数类型的参数分配给“void function()”类型的参数

不能将类型为“ combined”的参数分配给类型为“ FormatFn”的参数

无法将字符串类型的参数分配给paramMap

无法将函数分配给具有通用参数的类型

通过方法类型参数分配给类型成员的值会破坏类型等效性

rxjs6分区方法:无法将类型“ UnaryFunction”的参数分配给类型“ OperatorFunction”的参数