gpt4 book ai didi

angular - TS2339 : Property 'text' does not exist on type '{}'

转载 作者:行者123 更新时间:2023-12-05 09:34:19 28 4
gpt4 key购买 nike

我实际上面临着这一行问题:- <input [(ngModel)]="question.text" name="question" matInput placeholder="Question">这里 .text 扩展造成问题。这里我使用了 Angular11。这是我的代码。

问题.component.html

<mat-card >

<mat-card-content>

<form>

<mat-form-field>
<mat-label>Question</mat-label>
<input [(ngModel)]="question.text" name="question" matInput placeholder="Question">
</mat-form-field>

</form>
</mat-card-content>


<mat-card-actions>
<button (click)="post(question)" mat-button>POST</button>
</mat-card-actions>


</mat-card>

问题.component.ts

import {Component} from '@angular/core'
import {ApiService} from './api.service'

@Component({

selector:'question',
templateUrl:'./question.component.html'

})

export class QuestionComponent{

question = {}

constructor(private api:ApiService){}

post(question)
{
this.api.postQuestion(question);
}
}

当我运行我的应用程序时,我发现了这些错误:-

src/app/question.component.ts:7:16
7 templateUrl:'./question.component.html'
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component QuestionComponent.


Error: src/app/question.component.html:9:36 - error TS2339: Property 'text' does not exist on type '{}'.

9 <input [(ngModel)]="question.text" name="question" matInput placeholder="Question">
~~~~

src/app/question.component.ts:7:16
7 templateUrl:'./question.component.html'
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component QuestionComponent.

如何解决这个问题?

最佳答案

您必须声明以“question”命名的属性的类型。您可以尝试以下,

question:any = {};

这是最近更新版本的 typescript 严格类型检查问题。如果您使用的是 Angular,那么您可以通过这种方式轻松关闭这种严格的类型检查,Turn off strict type checking in Angular

关于angular - TS2339 : Property 'text' does not exist on type '{}' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66731356/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com