当初、私はこの初期化のカスタムクラス
import { CustomDate } from './../models/customDate';
export class DateService {
datePickerResultToDate(date: Date) {
var myDate: CustomDate;
var month_str: string;
switch (date.getMonth()) {
case 0:
month_str = 'Jan';
break;
case 1:
month_str = 'Feb';
break;
// etc...
default:
month_str = 'Some Error';
}
myDate.day = date.getDate();
myDate.month = date.getMonth();
myDate.year = date.getFullYear();
myDate.customDateString = date.getDate().toString() + ' ' + month_str + ' ' + date.getFullYear().toString();
return myDate
}
}
実行されると
export class CustomDate {
day: number;
month: number;
year: number;
customDateString: string;
}
が、私はこのエラーを取得しています
error_handler.jsのように見える機能を持っている:54例外:./TimeSheetComponentクラスのエラーTimeSheetComponent - インラインテンプレート:16:8原因:未定義のプロパティ 'day'を設定できません
だから、私は
var myDate: CustomDate = {
day: 0,
month: 0,
year: 0,
customDateString: ''
}
に変更する必要が考え出し、それはそれを修正します。私の質問は、それは私がそれを使用するたびにmyDateの値を初期化することは非常に奇妙なようです。これを単純化する方法はありますか?私はmyDateを使用したいときに、すでに初期化され、使用準備が整っていますか?それとも毎回これをやらなければならないのですか?