【问题标题】:Angular - How to resolve error TS2339: Property 'dob_year' does not exist on typeAngular - 如何解决错误 TS2339:类型上不存在属性“dob_year”
【发布时间】:2019-10-05 09:06:50
【问题描述】:

对于 Angular-7,我正在尝试使用 Angular 组件打字稿中的选择选项来生成出生日期,并分别生成日期、月份和年份。

client.component.ts

import { Component, OnInit, ElementRef, NgZone, ViewChild } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ApiService } from '../../shared/services/api.service';
import { TokenService } from '../../shared/services/token.service';
import { Router } from '@angular/router';

const getMonth = (idx) => {

  var objDate = new Date();
  objDate.setDate(1);
  objDate.setMonth(idx-1);

  var locale = "en-us",
      month = objDate.toLocaleString(locale, { month: "long" });

    return month;
}


@Component({
  selector: 'app-client-quotes-landing',
  templateUrl: './client-quotes-landing.component.html',
  styleUrls: ['./client-quotes-landing.component.scss']
})
export class ClientQuotesLandingComponent implements OnInit {

  public title = 'Places';
  public addrKeys: string[];
  public addr: object;

  months = Array(12).fill(0).map((i,idx) => getMonth(idx + 1));

  public form = {
    first_name : null,
    last_name : null,
    dob_day : null,
    dob_month : null,
    dob_year : null,
  };

  public error = {
    'first_name' : null,
    'last_name' : null,
    'dob_day' : null,
    'dob_month' : null,
    'dob_year' : null,
  };

  public get days() {
    const dayCount = this.getDaysInMonth(this.form.dob_year, this.form.dob_month);
    return Array(dayCount).fill(0).map((i,idx) => idx +1)
  }

  public getDaysInMonth(year: number, month: number) {
    return 32 - new Date(year, month - 1, 32).getDate();
  }

  constructor(
    private api: ApiService,
    private token: TokenService,
    private router: Router,
    private notify: SnotifyService,
    private zone: NgZone,
    ) {
     }

  ngOnInit() {

  }

}

我想从以下位置访问 dob_month 和 dob_year:

公开表格 = {...}

然后,我得到了这个错误:

src/app/pages/client-quotes-landing/client-quotes-landing.component.ts(94,47) 中的错误:错误 TS2339:“ClientQuotesLandingComponent”类型上不存在属性“dob_year”。 src/app/pages/client-quotes-landing/client-quotes-landing.component.ts(94,62):错误 TS2339:“ClientQuotesLandingComponent”类型上不存在属性“dob_month”。

错误来自:

  public get days() {
    const dayCount = this.getDaysInMonth(this.form.dob_year, this.form.dob_month);
    return Array(dayCount).fill(0).map((i,idx) => idx +1)
  }

如何解决此错误?

【问题讨论】:

    标签: angular


    【解决方案1】:

    问题似乎来自您声明的函数

    尝试删除函数的 get 和 days 关键字之间的空格

    喜欢

    public getdays() {
        const dayCount = this.getDaysInMonth(this.form.dob_year, this.form.dob_month);
        return Array(dayCount).fill(0).map((i,idx) => idx +1)
      }
    

    【讨论】:

    • Hitech,Ayobamile 想创建一个“getter”-在 html 中有一些类似 {{days}}-。这很奇怪,因为在获取日期之前已经定义了表单。另一种情况是 form.dob_year 为空,(因此您需要在函数 getDaysInMonth 中检查是否为空)。有没有像form=...这样的地方(如果我们想像if (form==null)?这样进行比较,可能会出现类型错误
    猜你喜欢
    • 2019-10-10
    • 1970-01-01
    • 2018-03-15
    • 2016-07-18
    • 2021-10-22
    • 2021-12-01
    • 1970-01-01
    • 2019-01-21
    • 2016-08-13
    相关资源
    最近更新 更多