'string' 类型的参数不能分配给 'Date' 类型的参数
Argument of type 'string' is not assignable to parameter of type 'Date'
我不太清楚我在日期方面哪里出错了。我收到错误:
"Argument of type 'string' is not assignable to parameter of type 'Date'. <button mat-raised-button type="button" (click)="createDamageAssessmentReport(DescOfReportInput.value, AuthorInput.value, DateInput.value)" color="primary">Create"
<mat-sidenav-content>
<div class="sect">
<h1>Damage Assessment Report</h1>
</div>
<mat-card>
<mat-card-header>Create report:</mat-card-header>
<br><br>
<mat-card-content>
<div>
<mat-form-field>
<mat-label>Report Description</mat-label>
<input
#DescOfReportInput
placeholder="Description of Report"
matInput type= "string"
required>
</mat-form-field>
<br>
<mat-form-field>
<mat-label>Author</mat-label>
<input
#AuthorInput
placeholder="Author"
matInput type= "string"
required>
</mat-form-field>
<br>
<mat-form-field>
<mat-label>Date</mat-label>
<input
#DateInput
placeholder="----/--/--"
matInput type= "Date"
required>
</mat-form-field>
<br>
</div>
<div>
<button mat-raised-button type="button" (click)="createDamageAssessmentReport(DescOfReportInput.value, AuthorInput.value, DateInput.value)" color="primary">Create</button>
</div>
</mat-card-content>
</mat-card>
</mat-sidenav-content>
关联的TypeScript文件如下:
import { Component, OnInit } from '@angular/core';
import { DamageAssessmentReportService } from 'src/app/damage-assessment-report.service';
@Component({
selector: 'app-fa-daform',
templateUrl: './fa-daform.component.html',
styleUrls: ['./fa-daform.component.css']
})
export class FADAFormComponent implements OnInit {
constructor(private damageAssessmentReportService : DamageAssessmentReportService) { }
//assessmentDescription: string, author: string, reportDateTime: Date
createDamageAssessmentReport(assessmentDescription: string, author: string, reportDateTime: Date){
this.damageAssessmentReportService
.createDAReport(assessmentDescription,author, reportDateTime)
.subscribe((response : any)=>{
console.log(response);
});
}
ngOnInit(): void { }
}
试试这个:
createDamageAssessmentReport(assessmentDescription: string, author: string, reportDateString: string){
const reportDateTime = new Date(reportDateString); // Here you should have your date ready to be used as you wish
this.damageAssessmentReportService
.createDAReport("New Report","Name", new Date(2020, 9, 10))
.subscribe((response : any)=>{
console.log(response);
});
}
我不太清楚我在日期方面哪里出错了。我收到错误:
"Argument of type 'string' is not assignable to parameter of type 'Date'. <button mat-raised-button type="button" (click)="createDamageAssessmentReport(DescOfReportInput.value, AuthorInput.value, DateInput.value)" color="primary">Create"
<mat-sidenav-content>
<div class="sect">
<h1>Damage Assessment Report</h1>
</div>
<mat-card>
<mat-card-header>Create report:</mat-card-header>
<br><br>
<mat-card-content>
<div>
<mat-form-field>
<mat-label>Report Description</mat-label>
<input
#DescOfReportInput
placeholder="Description of Report"
matInput type= "string"
required>
</mat-form-field>
<br>
<mat-form-field>
<mat-label>Author</mat-label>
<input
#AuthorInput
placeholder="Author"
matInput type= "string"
required>
</mat-form-field>
<br>
<mat-form-field>
<mat-label>Date</mat-label>
<input
#DateInput
placeholder="----/--/--"
matInput type= "Date"
required>
</mat-form-field>
<br>
</div>
<div>
<button mat-raised-button type="button" (click)="createDamageAssessmentReport(DescOfReportInput.value, AuthorInput.value, DateInput.value)" color="primary">Create</button>
</div>
</mat-card-content>
</mat-card>
</mat-sidenav-content>
关联的TypeScript文件如下:
import { Component, OnInit } from '@angular/core';
import { DamageAssessmentReportService } from 'src/app/damage-assessment-report.service';
@Component({
selector: 'app-fa-daform',
templateUrl: './fa-daform.component.html',
styleUrls: ['./fa-daform.component.css']
})
export class FADAFormComponent implements OnInit {
constructor(private damageAssessmentReportService : DamageAssessmentReportService) { }
//assessmentDescription: string, author: string, reportDateTime: Date
createDamageAssessmentReport(assessmentDescription: string, author: string, reportDateTime: Date){
this.damageAssessmentReportService
.createDAReport(assessmentDescription,author, reportDateTime)
.subscribe((response : any)=>{
console.log(response);
});
}
ngOnInit(): void { }
}
试试这个:
createDamageAssessmentReport(assessmentDescription: string, author: string, reportDateString: string){
const reportDateTime = new Date(reportDateString); // Here you should have your date ready to be used as you wish
this.damageAssessmentReportService
.createDAReport("New Report","Name", new Date(2020, 9, 10))
.subscribe((response : any)=>{
console.log(response);
});
}