【问题标题】:Property 'endDate' does not exist on type on the component组件的类型上不存在属性“endDate”
【发布时间】:2020-10-23 08:56:49
【问题描述】:

属性endDate 不存在于类型TestComponent

test.Component.cs

--

import { Component} from '@angular/core';
import { Router } from '@angular/router'
import {Message} from 'primeng/api';
import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';

@Component({
  templateUrl: './test.component.html',
  selector: 'test',
  providers:[TestService]
})
export class TestComponent  {
    public testingForm: FormGroup;

    constructor(
        private router: Router,
        private userService: UserService,
        ,private formBuilder: FormBuilder
      ) { 
        console.log("Inside constructor...");
      }
      
      ngOnInit():void {  
           console.log("Inside onNotify ...."); 
           this.testingForm = this.formBuilder.group({
            startDate: new FormControl('', Validators.required), 
            endDate: new FormControl('', Validators.required), 
            message: new FormControl('', Validators.required)
           });     
           this.loadData();       
      }
      
      ...////.....
      
      
      }

--

test.component.html

--

<form [formGroup]="testingForm"  >
<div>
<label class="control-label col-sm-4">Start Date:</label>
<div class="col-sm-8">
    <p-calendar formControlName="endDate" [showIcon]="true"></p-calendar><span style="margin-left:35px">{{endDate|date}}</span>
</div>
</div>
</form>

app.module.ts - 已导入 formModule

import { FormsModule, ReactiveFormsModule } from '@angular/forms';

以下是我得到的错误:

 ERROR in src/testing/test.component.html:37:115 - error TS2339: Property 'endDate' does not exist on type 'testingComponent'.

    37             <p-calendar formControlName="endDate" [showIcon]="true"></p-calendar><span style="margin-left:35px">{{endDate|date}}</span>
                                                                                                                         ~~~~~~~

      src/testing/test.component.ts:19:16
        19   templateUrl: './test.component.html',
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        Error occurs in the template of component TestComponent.

非常感谢任何输入。 TIA

【问题讨论】:

    标签: angular typescript primeng angular-directive


    【解决方案1】:

    没错,它没有。

    例如,您可以添加 getter 以从 FormGroup 中提取它:

    get endDate(): Date {
        return this.testingForm ? this.testingForm.value.endDate : null;
    }
    

    【讨论】:

    • 我在编译期间看到了错误,但运行时显示了日历。我还没有提交页面。
    • 是的,开发服务器比生产版本更宽松。
    猜你喜欢
    • 2021-02-18
    • 1970-01-01
    • 2021-04-25
    • 2017-02-13
    • 2017-08-15
    • 2021-01-09
    • 1970-01-01
    • 2021-07-10
    相关资源
    最近更新 更多