简体   繁体   English

如何在Angular 2中使用Typescript类和接口实现“模式”以进行表单验证?

[英]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上关于数据驱动的表单和表单控件定义(一些讨论12 ,和3 )。

As an example, I am trying to roll a registration form for user registration. 举例来说,我正在尝试为用户注册使用注册表格。 The requirements are as follows: 要求如下:

  1. Need a User object as a client observable, and 3 additional fields for registration to the backend 需要一个User对象作为客户端可观察到的对象,并需要3个其他字段才能注册到后端

  2. 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错误)

  3. 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. 我目前将所有字段设置为可选,因为当未提供某些参数时,我的应用程序将无法编译。

  1. What is the difference between class and interface in TypeScript/ES6? TypeScript / ES6中的classinterface什么区别? 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的示例),但是我不确定是否应该在这里使用classinterface Most examples I see just use class . 我看到的大多数示例只是使用class
  2. Is it correct style to use the ? 使用正确的样式? optional param here? 可选参数在这里? The observable gets User values at three different stages, and TypeScript kicks out an error when I don't have all the values. 可观察对象在三个不同的阶段获取用户值,并且当我没有所有值时,TypeScript会引发错误。 I've seen examples where some sort of 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对象想要的内容。
  3. Is there any way to set options like the pattern referenced in #3? 有没有办法设置选项,例如#3中引用的模式? The options are set in a constructor like 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: 我可以回答第一个问题:

  • An interface provides the definition of a type. 接口提供类型的定义。
  • A class defines an implementation of a type. 类定义类型的实现。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM