我使用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'.在我和其他属性有同样的问题之前,因为我使用了“整型”,但是我把它改成了“数字”,所以我想知道这是否是一个角度不理解特定类型属性的问题。但是在网上搜索之后,我还没有找到如何在角上声明一个“日期”类型的变量。有什么方法来声明日期类型的变量吗?或者我应该用一个字符串,并有某种处理来使用它作为一个日期?
发布于 2017-04-19 18:46:13
这是一个打字本问题。在类型记录中没有date类型。您可以使用Date来指示本机javascript对象,但这只是字段的类型。角形/TS不会给你带来任何魔法来强迫你的值变成一个日期对象。如果数据来自JSON源,则日期通常只是字符串(JSON不支持日期对象)。
发布于 2017-04-20 08:54:19
您可以使用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;
}发布于 2017-04-19 18:49:35
请检查打字本中支持的数据类型.
https://www.typescriptlang.org/docs/handbook/basic-types.html
日期数据类型在那里不可用。您可以根据需要使用、string、或任何数据类型。
https://stackoverflow.com/questions/43503623
复制相似问题