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

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

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

问题

  1. 我正在尝试使用一组评分轮次的名称填充 mat-select 列表。
  2. 这里我定义了评分轮次是什么:
  3. ```typescript
  4. scoring.rounds.ts
  5. export interface ScoringRounds {
  6. id: number;
  7. name: string;
  8. arrowsPerEnd: number;
  9. numberOfEnds: number;
  10. distanceOne: string;
  11. endsOne: number;
  12. faceOne: string;
  13. distanceTwo?: string;
  14. endsTwo?: number;
  15. faceTwo?: string;
  16. distanceThree?: string;
  17. endsThree?: number;
  18. faceThree?: string;
  19. distanceFour?: string;
  20. endsFour?: number;
  21. faceFour?: string;
  22. scoreType: string;
  23. }

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

  1. scoring-rounds.service.ts
  2. import { Injectable } from '@angular/core';
  3. import { ScoringRounds } from './scoring.rounds';
  4. @Injectable({
  5. providedIn: 'root'
  6. })
  7. export class ScoringRoundsService {
  8. protected scoringRoundsList: ScoringRounds[] = [
  9. {id: 1, name: 'WA50', arrowsPerEnd: 6, numberOfEnds: 12,
  10. distanceOne: '50m', endsOne: 12, faceOne: '122cm',
  11. scoreType: 'Metric 10 Zone'},
  12. {id: 2, name: 'WA1440', arrowsPerEnd: 6, numberOfEnds: 24,
  13. distanceOne: '90m', endsOne: 6, faceOne: '122cm',
  14. distanceTwo: '70m', endsTwo: 6, faceTwo: '122cm',
  15. distanceThree: '50m', endsThree: 6, faceThree: '80cm',
  16. distanceFour: '30m', endsFour: 6, faceFour: '80cm',
  17. scoreType: 'Metric 10 Zone'}
  18. ]
  19. getAllScoringRoundNames(): string[] {
  20. return this.scoringRoundsList.map(round => round.name);
  21. }
  22. }

当尝试将 .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 中明确定义了它。

  1. <details>
  2. <summary>英文:</summary>
  3. I am trying to populate a mat-select list with the names of a set of scoring rounds.
  4. 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;
}

  1. 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;
}

}

  1. 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.
  2. &gt;[{
  3. &gt;&gt;&quot;resource&quot;: &quot;/c:/TM470/target-club/src/app/add-scores/add-scores.component.html&quot;,
  4. &quot;owner&quot;: &quot;_generated_diagnostic_collection_name_#0&quot;,
  5. &quot;code&quot;: &quot;2339&quot;,
  6. &quot;severity&quot;: 8,
  7. &quot;message&quot;: &quot;Property &#39;name&#39; does not exist on type &#39;ScoringRounds[]&#39;.&quot;,
  8. &quot;source&quot;: &quot;ngtsc&quot;,
  9. &quot;startLineNumber&quot;: 20,
  10. &quot;startColumn&quot;: 88,
  11. &quot;endLineNumber&quot;: 20,
  12. &quot;endColumn&quot;: 92,
  13. &quot;relatedInformation&quot;: [
  14. {
  15. &quot;startLineNumber&quot;: 19,
  16. &quot;startColumn&quot;: 8,
  17. &quot;endLineNumber&quot;: 19,
  18. &quot;endColumn&quot;: 37,
  19. &quot;message&quot;: &quot;Error occurs in the template of component AddScoresComponent.&quot;,
  20. &quot;resource&quot;: &quot;/c:/TM470/target-club/src/app/add-scores/add-scores.component.ts&quot;
  21. }
  22. ]
  23. }]
  24. 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
  25. </details>
  26. # 答案1
  27. **得分**: 0
  28. `this.scoringRoundsList` 是一个包含 `ScoringRounds` 的数组,但它们没有 `name` 属性。如果你想获取所有 `scoringRounds` `name`,可以尝试以下代码:
  29. ```typescript
  30. getAllScoringRoundNames(): string[] {
  31. return this.scoringRoundsList.map(round => round.name);
  32. }

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

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

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

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
  1. &lt;mat-form-field&gt;
  2. &lt;mat-label&gt;Round Name&lt;/mat-label&gt;
  3. &lt;mat-select formControlName=&quot;roundName&quot; [(ngModel)]=&quot;scoringRoundsList&quot;&gt;
  4. &lt;mat-option *ngFor=&quot;let round of scoringRoundsList&quot; [value]=&quot;round.name&quot;&gt;
  5. {{round.name}}
  6. &lt;/mat-option&gt;
  7. &lt;/mat-select&gt;
  8. &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:

确定