web-dev-qa-db-ja.com

angular2モデルで日付属性を宣言する方法

Rails 5 + Angular2を使用してWebアプリケーションを作成していますが、「Books」というモデルがあります。私の問題は、「books.ts」というファイルがあり、コード:

export class Book {
id: number;
author_one: string;
author_two: string;
author_three: string;
title: string;
subtitle: string;
publisher: string;
year: date;
city: string;
edition: number;
volume: number;
pages: number;
ISBN: string;
barcode: string;
}

しかし、「ng serve --port 9000」を実行すると、次のエラーが発生しました。

Cannot find name 'date'.

「整数」を使用していたため、他の属性で同じ問題が発生する前に、「数値」に変更して機能したため、Angular =特定のタイプの属性を理解していませんが、インターネットを検索した後、Angularで「date」タイプの変数を宣言する方法を見つけられませんでした。dateタイプの変数を宣言する方法はありますか?日付としてそれを使用する何らかの治療?

8
Makrau

これはTypeScriptの質問です。 TypeScriptにはdate型はありません。 Dateを使用して、ネイティブのjavascript Dateオブジェクトを示すことができますが、これはフィールドの単なる入力です。 Angular/TSは、値をDateオブジェクトに強制するマジックを行いません。データがJSONソースからのものである場合、日付は通常単なる文字列です(JSONは日付オブジェクトをサポートしていません)。

20
adharris

TypeScriptでサポートされているデータ型を確認してください。

https://www.typescriptlang.org/docs/handbook/basic-types.html

dateデータ型は利用できません。必要に応じて、stringまたはanyデータ型を使用できます。

5

Moment.jsを使用すると、簡単に実行できます。

import { Moment } from 'moment';

export class Book {
    id: number;
    author_one: string;
    author_two: string;
    author_three: string;
    title: string;
    subtitle: string;
    publisher: string;
    year: Moment;
    city: string;
    edition: number;
    volume: number;
    pages: number;
    ISBN: string;
    barcode: string;
}
2
Danu Akbar

javaScriptには日付タイプがありません。 日付 javascriptタイプを使用してください。

0