[英]How to implement a “schema” with Typescript classes and interfaces in Angular 2 for form validation?
I am currently learning TypeScript and the Angular 2 framework. 我目前正在学习TypeScript和Angular 2框架。 I have read through some of Dan Wahlin's articles ( here and here ) on TypeScript Classes and Interfaces, and have further seen some discussions on Github about data driven forms and form control definitions ( 1 , 2 , & 3 ).
我已经通过一些丹Wahlin制品(读这里和这里 )的打字稿类和接口,并进一步看到在Github上关于数据驱动的表单和表单控件定义(一些讨论1 , 2 ,和3 )。
As an example, I am trying to roll a registration form for user registration. 举例来说,我正在尝试为用户注册使用注册表格。 The requirements are as follows:
要求如下:
Need a User object as a client observable, and 3 additional fields for registration to the backend 需要一个User对象作为客户端可观察到的对象,并需要3个其他字段才能注册到后端
The registration is multi-step and occurs on multiple templates. 注册是多步骤的,并且在多个模板上进行。 As such, when a
new User
so I think I need optional operators or a base object that is null (to avoid TypeScript errors when app is compiling) 因此,当我有一个
new User
,我认为我需要可选的运算符或为null的基础对象(以避免在应用编译时出现TypeScript错误)
I would like to set options for some of the fields (ie gender is 'male' or 'female') similar to the way Dan sets IAutoOptions
in the constructor for Auto
in this article . 我想为某些字段设置选项(即,性别是“男性”还是“女性”),类似于Dan在本文的
Auto
构造函数中设置IAutoOptions
的方式。 I know I can set these with ngForm, ControlGroups, or the FormBuilder, but I would like to know if there is a way to do it with TypeScript so I can make it accessible to an "appState" observable. 我知道我可以使用ngForm,ControlGroups或FormBuilder进行设置,但我想知道是否可以使用TypeScript进行设置,以便可以使“ appState”可访问。
I currently have those set up as this set up as follows: 我目前已经按照以下方式进行了设置:
export interface User {
gender?: string;
seeking?: string;
email?: string;
username?: string;
birth_date?: Date;
country?: string;
zipcode?: string;
}
export interface BackendUser extends User {
password?: string;
password_confirm?: string;
agreed?: boolean;
}
The base User
interface is needed throughout the app, and BackendUser
interface that is extended to provide three additional fields during registration. 在整个应用程序中都需要基本的
User
界面,并且扩展了BackendUser
界面以在注册期间提供三个附加字段。 I currently have all of the fields set to optional because my app wouldn't compile when certain params weren't provided. 我目前将所有字段设置为可选,因为当未提供某些参数时,我的应用程序将无法编译。
class
and interface
in TypeScript/ES6? class
和interface
什么区别? I think I am using extends
properly here (per Wahlin's example), but I am not sure if I should be using class
or interface
here. extends
(按照Wahlin的示例),但是我不确定是否应该在这里使用class
或interface
。 Most examples I see just use class
. class
。 ?
?
optional param here? null
user object is initiated on startup and later has it's properties overridden, but am not sure if this is best practice or what that null
object would like like in this instance. null
用户对象在启动时启动,后来又覆盖了它的属性,但是不确定这是最佳实践还是在这种情况下该null
对象想要的内容。 this.engine = options.engine;
this.engine = options.engine;
这样的构造函数中设置的this.engine = options.engine;
, but i don't see where options.engine
comes from. options.engine
来源。 Any advice or a point in the right direction is greatly appreciated!! 任何建议或朝着正确方向的观点将不胜感激!! Thanks!
谢谢!
I can answer the first question: 我可以回答第一个问题:
So it is possible to have both a Product interface and a Product class, for example. 因此,例如可以同时具有Product接口和Product类。 The interface defines the set of valid properties (and optionally methods) that are required for a particular type.
该接口定义了特定类型所需的一组有效属性(以及可选的方法)。 A class can then implement that interface and provide the code required to get/set values and for all methods.
然后,一个类可以实现该接口,并提供获取/设置值以及所有方法所需的代码。
The general rule of thumb I have seen is that if you want to strongly type your data, but have no real methods, then just use an interface. 我所看到的一般经验法则是,如果您想强类型化数据,但是没有真正的方法,则只需使用接口即可。 For example, if your code manages an array of products, then you can define a product interface to strongly type those products.
例如,如果您的代码管理一系列产品,则可以定义产品接口以强类型化那些产品。
If you also have operations, such as to calculate a minimum price based on several product properties or to calculate a standard discount, then a class can implement those operations. 如果您还有其他操作,例如根据多个产品属性计算最低价格或计算标准折扣,则类可以实现这些操作。
(I didn't use your "user" example to prevent the need to reference a "user interface" and cause confusion.) (我没有使用您的“用户”示例来避免需要引用“用户界面”并引起混乱。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.