【问题标题】:Element implicitly has an 'any' type because expression of type in ANGULAR元素隐含地具有“任何”类型,因为 ANGULAR 中的类型表达式
【发布时间】:2021-04-29 11:21:59
【问题描述】:

我在更新列出的表单时遇到错误,ReactForm 这是我附加的代码请帮我解决这个问题...提前致谢 我的代码::

import { CommonService } from './../common.service';
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-update-resturant',
  templateUrl: './update-resturant.component.html',
  styleUrls: ['./update-resturant.component.css']
})
export class UpdateResturantComponent implements OnInit {
  alert : Boolean = false;
  updateResto = new FormGroup({
    Name: new FormControl(''),
    Email: new FormControl('')
  })

  constructor(private resto:CommonService, private router: ActivatedRoute) { }

  ngOnInit(): void {
    this.resto.getCurrentData(this.router.snapshot.params.id).subscribe((result)=> {
      console.log(result)
      this.updateResto = new FormGroup({
        Name: new FormControl(result['Name']),
        Email: new FormControl(result['Email'])
    })
  }
  }

错误::

元素隐式具有“任何”类型,因为“电子邮件”类型的表达式不能用于索引类型“对象”。 “对象”类型上不存在属性“电子邮件”。

服务代码::

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class CommonService {
URL= "http://localhost:3000/list"

  constructor(private _http:HttpClient) { }
  getRestoList(){
    return this._http.get(this.URL)
  }

  addResto(data: any){
    return this._http.post(this.URL, data);
  }

  // deleteCurrentData(id: any){
  //   return this._http.delete(`${this.URL}/${id}`)
  // }

  getCurrentData(id: number){
    return this._http.get(`${this.URL}/${id}`)
  }
}

【问题讨论】:

  • 您应该只更新 this.updateResto elems 值,而是重新创建它们。
  • 检查以下Plunker
  • 通过将我的代码更新为您提到的代码,正在编译 IT,但没有将后端值放入表单,它只是在栏上显示 Oject 对象 ngOnInit(): void { this.resto .getCurrentData(this.router.snapshot.params.id).subscribe((result)=> { console.log(result) this.updateResto.controls['Name'].setValue(result), this.updateResto.controls[ '电子邮件'].setValue(result) }) }

标签: angular


【解决方案1】:

您无需重新创建 FormGroup,只需更新 FormControl 值即可。结帐Reactive Form Guides

ngOnInit(): void {
    this.resto.getCurrentData(this.router.snapshot.params.id).subscribe((result)=> {
      this.updateResto.controls['Name'].setValue(result['Name']);
      this.updateResto.controls['Email'].setValue(result['Email']);

      // OR you can update them all at once
      this.updateResto.setValue({
        Name: result['Name'],
        Email: result['Email']
    })
  }

请参阅以下example 以供参考。

【讨论】:

  • 谢谢您,先生,这解决了我的错误... :) 感谢您的快速响应先生
【解决方案2】:

更新:
首先在 OnInit 中分配您的表单组:

ngOnInit(): void {
  this.updateResto = new FormGroup({
    // name form controls in lowerCamelCase
    name: new FormControl(''),
    email: new FormControl(''),
  });

  // then make a query to your backend API
  this.resto.getCurrentData(this.router.snapshot.params.id).subscribe((result)=> {
      // I just gave wrong answer, since formGroup.value's are readonly properties
      // to update values use formGroup.patchValue():
      this.updateResto.patchValue({
        name: result.whatevervalue,
        email: result.whatevervalue2
      });
  })
}

在类的顶部,您只需定义变量 updateResto:

// ! is for strict mode, typescript throws errors that your variable was not defined
updateResto!: FormGroup;

根据您的代码,当您输入组件时,您已经创建了 FormGroup,当您的服务返回值时,您尝试分配新的表单组

【讨论】:

  • 错误:src/app/update-resturant/update-resturant.component.ts:23:44 - 错误 TS2339:“对象”类型上不存在属性“名称”。 23 this.updateResto.value.Name = 结果.Name; ~~~~ src/app/update-resturant/update-resturant.component.ts:24:45 - 错误 TS2339:“对象”类型上不存在属性“电子邮件”。 24 this.updateResto.value.Email = 结果.Email;
  • 先生仍然面临同样的错误。
  • 虽然此代码可能会为问题提供解决方案,但最好添加有关其工作原理/方式的上下文。这可以帮助未来的用户学习并将这些知识应用到他们自己的代码中。在解释代码时,您也可能会以赞成票的形式从用户那里获得积极的反馈。
  • 按照您的建议更新了我的代码,但仍然面临同样的错误
  • 错误:src/app/update-resturant/update-resturant.component.ts:25:22 - 错误 TS2339:“对象”类型上不存在属性“名称”。 25 名称:result.name,~~~~ src/app/update-resturant/update-resturant.component.ts:26:23 - 错误 TS2339:“对象”类型上不存在属性“电子邮件”。 26 封电子邮件:result.email
猜你喜欢
  • 2021-08-16
  • 2021-10-27
  • 1970-01-01
  • 2023-02-07
  • 2018-04-25
  • 2021-09-23
  • 2023-04-01
  • 2021-08-24
相关资源
最近更新 更多