【发布时间】: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: '' } ;