属性在类型上不存在 – 但类型已定义了此属性

huangapple go评论61阅读模式
英文:

Property does not exist on type - but type has this property defined

问题

我正在尝试使用一组评分轮次的名称填充 mat-select 列表。

这里我定义了评分轮次是什么:

```typescript
scoring.rounds.ts
export interface ScoringRounds {
    id: number; 
    name: string;
    arrowsPerEnd: number;
    numberOfEnds: number;
    distanceOne: string;
    endsOne: number;
    faceOne: string;
    distanceTwo?: string;
    endsTwo?: number;
    faceTwo?: string;
    distanceThree?: string;
    endsThree?: number;
    faceThree?: string;
    distanceFour?: string;
    endsFour?: number;
    faceFour?: string;
    scoreType: string;
}

这里我提供了一个用于检索轮次列表的服务:

scoring-rounds.service.ts

import { Injectable } from '@angular/core';
import { ScoringRounds } from './scoring.rounds';

@Injectable({
  providedIn: 'root'
})
export class ScoringRoundsService {

  protected scoringRoundsList: ScoringRounds[] = [
    {id: 1, name: 'WA50', arrowsPerEnd: 6, numberOfEnds: 12, 
    distanceOne: '50m', endsOne: 12, faceOne: '122cm',
    scoreType: 'Metric 10 Zone'},
    {id: 2, name: 'WA1440', arrowsPerEnd: 6, numberOfEnds: 24, 
    distanceOne: '90m', endsOne: 6, faceOne: '122cm', 
    distanceTwo: '70m', endsTwo: 6, faceTwo: '122cm', 
    distanceThree: '50m', endsThree: 6, faceThree: '80cm',
    distanceFour: '30m', endsFour: 6, faceFour: '80cm',
    scoreType: 'Metric 10 Zone'}
  ]

  getAllScoringRoundNames(): string[] {
    return this.scoringRoundsList.map(round => round.name);
  }
}

当尝试将 .name 添加到返回语句中,或者在 HTML 中尝试将其用作 mat-select 的值时,我收到以下错误:

[{
>>"resource": "/c:/TM470/target-club/src/app/add-scores/add-scores.component.html",
"owner": "generated_diagnostic_collection_name#0",
"code": "2339",
"severity": 8,
"message": "Property 'name' does not exist on type 'ScoringRounds[]'.",
"source": "ngtsc",
"startLineNumber": 20,
"startColumn": 88,
"endLineNumber": 20,
"endColumn": 92,
"relatedInformation": [
{
"startLineNumber": 19,
"startColumn": 8,
"endLineNumber": 19,
"endColumn": 37,
"message": "Error occurs in the template of component AddScoresComponent.",
"resource": "/c:/TM470/target-club/src/app/add-scores/add-scores.component.ts"
}
]
}]

我尝试了多种方法,但无法理解为什么它说属性不存在,当我在 scoring.rounds.ts 中明确定义了它。


<details>
<summary>英文:</summary>

I am trying to populate a mat-select list with the names of a set of scoring rounds. 

Here I define what a scoring round is: 

scoring.rounds.ts
export interface ScoringRounds {
id: number;
name: string;
arrowsPerEnd: number;
numberOfEnds: number;
distanceOne: string;
endsOne: number;
faceOne: string;
distanceTwo?: string;
endsTwo?: number;
faceTwo?: string;
distanceThree?: string;
endsThree?: number;
faceThree?: string;
distanceFour?: string;
endsFour?: number;
faceFour?: string;
scoreType: string;
}


Here I provide a service to retrieve the list of rounds:

scoring-rounds.service.ts

import { Injectable } from '@angular/core';
import { ScoringRounds } from './scoring.rounds';

@Injectable({
providedIn: 'root'
})
export class ScoringRoundsService {

protected scoringRoundsList: ScoringRounds[] = [
{id: 1, name: 'WA50', arrowsPerEnd: 6, numberOfEnds: 12,
distanceOne: '50m', endsOne: 12, faceOne: '122cm',
scoreType: 'Metric 10 Zone'},
{id: 2, name: 'WA1440', arrowsPerEnd: 6, numberOfEnds: 24,
distanceOne: '90m', endsOne: 6, faceOne: '122cm',
distanceTwo: '70m', endsTwo: 6, faceTwo: '122cm',
distanceThree: '50m', endsThree: 6, faceThree: '80cm',
distanceFour: '30m', endsFour: 6, faceFour: '80cm',
scoreType: 'Metric 10 Zone'}

]

getAllScoringRoundNames(): ScoringRounds[] {
return this.scoringRoundsList.name;
}

}


and I receive the following error when adding .name to the return, or when trying to use it in the html to provide the values for mat-select. 



&gt;[{
	&gt;&gt;&quot;resource&quot;: &quot;/c:/TM470/target-club/src/app/add-scores/add-scores.component.html&quot;,
	&quot;owner&quot;: &quot;_generated_diagnostic_collection_name_#0&quot;,
	&quot;code&quot;: &quot;2339&quot;,
	&quot;severity&quot;: 8,
	&quot;message&quot;: &quot;Property &#39;name&#39; does not exist on type &#39;ScoringRounds[]&#39;.&quot;,
	&quot;source&quot;: &quot;ngtsc&quot;,
	&quot;startLineNumber&quot;: 20,
	&quot;startColumn&quot;: 88,
	&quot;endLineNumber&quot;: 20,
	&quot;endColumn&quot;: 92,
	&quot;relatedInformation&quot;: [
		{
			&quot;startLineNumber&quot;: 19,
			&quot;startColumn&quot;: 8,
			&quot;endLineNumber&quot;: 19,
			&quot;endColumn&quot;: 37,
			&quot;message&quot;: &quot;Error occurs in the template of component AddScoresComponent.&quot;,
			&quot;resource&quot;: &quot;/c:/TM470/target-club/src/app/add-scores/add-scores.component.ts&quot;
		}
	]
}]

I have tried multiple methods but cannot understand why this is saying the property does not exist, when I have explicitly defined it in scoring.rounds.ts

</details>


# 答案1
**得分**: 0

`this.scoringRoundsList` 是一个包含 `ScoringRounds` 的数组,但它们没有 `name` 属性。如果你想获取所有 `scoringRounds` 的 `name`,可以尝试以下代码:

```typescript
getAllScoringRoundNames(): string[] {
  return this.scoringRoundsList.map(round => round.name);
}

在你的表单中,使用下面的标记。请注意:

  • 我将轮次的 name 绑定为每个 mat-optionvalue - 这将使得点击时选择这个轮次,而不是整个数组
  • 也将其添加到 mat-option 组件内部 - 这会在下拉菜单中显示名称
<mat-form-field>
  <mat-label>轮次名称</mat-label>
  <mat-select formControlName="roundName" [(ngModel)]="scoringRoundsList">
    <mat-option *ngFor="let round of scoringRoundsList" [value]="round.name">
      {{round.name}}
    </mat-option>
  </mat-select>
</mat-form-field>

如果你有进一步的问题,请参考Angular Material文档

英文:

this.scoringRoundsList is an array of ScoringRounds, which does not have the name property. If you want to get the name for all the scoringRounds, try this:

getAllScoringRoundNames(): string[] {
  return this.scoringRoundsList.map(round =&gt; round.name);
}

In your form, use the markup below. Notice:

  • I am binding the round's name as the value of each mat-option - this will make this round get selected on click, instead of the array
  • Add it inside the mat-option component as well - this displays the name in the dropdown menu that you see
&lt;mat-form-field&gt;
  &lt;mat-label&gt;Round Name&lt;/mat-label&gt;
  &lt;mat-select formControlName=&quot;roundName&quot; [(ngModel)]=&quot;scoringRoundsList&quot;&gt;
    &lt;mat-option *ngFor=&quot;let round of scoringRoundsList&quot; [value]=&quot;round.name&quot;&gt;
      {{round.name}}
    &lt;/mat-option&gt;
  &lt;/mat-select&gt;
&lt;/mat-form-field&gt;

Refer to the Angular Material documentation if you have any further questions.

huangapple
  • 本文由 发表于 2023年7月3日 21:42:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定