【问题标题】:error TS2339: Property 'value' does not exist on type 'ElementRef<any>'错误 TS2339:“ElementRef<any>”类型上不存在属性“值”
【发布时间】:2021-06-21 08:15:08
【问题描述】:

我有这个问题:

错误 TS2339:“ElementRef”类型上不存在属性“值”。

我有一个包含此代码的 .html 文件:

<ion-content>
  <ion-input placeholder="Enter Name" type="text" #name name="name" ></ion-input>
  <ion-input placeholder="Enter Surname" type="text" #surname name="surname" ></ion-input>
  <ion-input placeholder="Enter Type" type="text" #type name="type" ></ion-input>
  <ion-button (click)="addNewUser()">Add New User</ion-button>

还有一个包含此代码的文件 .ts:

export class FirstPage implements OnInit {

  springUsers: SpringUser[];
  @ViewChild('name') name: ElementRef;
  @ViewChild('surname') surname: ElementRef;
  @ViewChild('type') type: ElementRef;
  
  ///[...]
  
  
  addNewUser(){
    console.log('addNewUser');
    let tempUser: SpringUser;
    tempUser = {
      name: this.name.value,
      surname: this.surname.value,
      type: this.type.value,
      email: 'email@gmail.com',
      password: '',
      passwordToVerify: ''

    } ;

    tempUser.name = this.name.value;
    tempUser.surname = this.surname.value;
    tempUser.type = this.type.value;

    this.springUserService.postSpringUser(tempUser).subscribe(data => {
      console.log('inserted user', data);
    });

【问题讨论】:

  • 按照文档 (angular.io/api/core/ElementRef#properties) 尝试 this.name.nativeElement.value。这基本上适用于您所有的ElementRef 电话。
  • 在每个输入中添加[(ngModel)] ?
  • 现在程序已编译,但我遇到了这个问题:错误类型错误:无法读取 FirstPage.addNewUser (first.page.ts:33) 处未定义的属性“值”,其中:first.page.ts: 33 是:addNewUser(){ console.log('addNewUser');让 tempUser: SpringUser; tempUser = { name: this.name.nativeElement.value, // 第 33 行 surname: this.surname.nativeElement.value, type: this.type.nativeElement.value, email: 'email@gmail.com', password: ' ', passwordToVerify: '' } ;

标签: angular ionic-framework


【解决方案1】:

按照文档 (docs) 尝试 this.name.nativeElement.value。这基本上适用于您所有的ElementRef 电话。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-13
    • 2018-10-25
    • 2018-08-02
    • 2017-10-06
    • 2019-01-21
    • 2016-08-13
    • 2021-10-22
    • 1970-01-01
    相关资源
    最近更新 更多