Angular 2 - 打字稿在对象点符号上窒息,期望';'

Angular 2 - Typescript choking on object dot notation, expects ';'

我是 TypeScript 和 Angular 2 的新手。我正在将一个项目从 Angular 1 转移过来,但我无法获得我的一些 属性 定义到 Angular 2 组件中。

我有这些 属性 定义:

import { Component } from 'angular2/core';
@Component({
  selector: 'contact-detail',
  templateUrl: 'app/contacts/contact-detail.template.html',
  styleUrls: ['app/contacts/contact-detail.style.css']
})
export class ContactDetailComponent {
  contactFormOptions = {};
  contactFormOptions.initDateDefault = '';
  contactFormOptions.followUpDateDefault = '';
  contactBasicInfo: {};
  contactBasicInfo.bdMonth = '0';
  contactBasicInfo.bdDay = '0';
}

Angular 1 很适合我以这种方式定义对象属性(如 contactFormOptions.initDateDefault = '';)。使用点语法。但是,Typescript 会在这些属性的点符号上窒息并表示 ; 是预期的。为什么?我错过了什么?

谢谢!

要让 TypeScript 理解 moment.js,您需要一个定义文件。幸运的是,moment.js 提供了一个,你只需要添加引用,这里也已经弄清楚了,在 Defintely Typed:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/moment/moment.d.ts

为了清楚起见,需要添加到代码中的是:

declare var moment: moment.MomentStatic;

好吧,我是 OOP 世界的新手。我需要做的是声明属性及其类型,然后在构造函数(或方法)中分配值。

export class ContactDetailComponent {
  contactFormOptions: any = {};
  contactBasicInfo: any = {};

  constructor ( ) {
    this.contactFormOptions.initDateDefault = moment();
    this.contactFormOptions.followUpDateDefault = moment().add(7, 'days');
    this.contactBasicInfo.bdMonth = '0';
    this.contactBasicInfo.bdDay = '0';
  }
}