【发布时间】:2021-06-18 05:32:03
【问题描述】:
我实际上面临这条线问题:- <input [(ngModel)]="question.text" name="question" matInput placeholder="Question">here .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>
question.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 = {}你没有给它一个类型,所以编译器不知道它有一个文本属性。 Typescript 不是 Javascript。只需将该属性初始化为一个空字符串或任何它应该是的。
标签: angular typescript angular-material angular11