typescript Angular 2 模型:接口与类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43809031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:30:55  来源:igfitidea点击:

Angular 2 models: interface vs classes

angulartypescript

提问by Tomas Eglinskas

I have a login/registration module and I want to write a model for them.

我有一个登录/注册模块,我想为它们编写一个模型。

I would use interfaces, but I need to have one predefined value.

我会使用接口,但我需要有一个预定义的值。

And I was wondering - should I put the predefined out as a constant (it won't be changed, altered) and use interfaces. Or write it as classes (as currently)

我想知道 - 我是否应该将预定义的内容作为常量(它不会被更改、更改)并使用接口。或将其写为类(如当前)

Currently, I wrote two separate classes - registration.model.ts, login.model.ts, could I be able to abstract to use only one model? (example: user.model.ts)

目前,我写了两个独立的类——registration.model.ts、login.model.ts,我可以抽象为只使用一个模型吗?(例如:user.model.ts)

Some examples:

一些例子:

export class LoginUser {
  constructor(
    private email,
    private password,

    // I need to have it, backend expects it to be sent
    private connection = 'Username-Password-Authentication'  
  ) { }
} 


export class RegistrateUser {
  constructor(
    public name: string,
    public lastName: string,
    public email: string,
    public password: string,

    // I need to have it, backend expects it to be sent
    private connection = 'Username-Password-Authentication'
  ) { }
}

采纳答案by Aluan Haddad

I would definitely use interfaces. It is a simple approach and follows the TypeScript recommendations for working with JSON.

我肯定会使用接口。这是一种简单的方法,并遵循使用 JSON 的 TypeScript 建议。

The property in question,

有问题的财产,

private connection = 'Username-Password-Authentication';

can well be added by the service that performs the request.

可以很好地由执行请求的服务添加。

This will also reduce code duplication because you can use a generic function or service to create this request object.

这也将减少代码重复,因为您可以使用通用函数或服务来创建此请求对象。

For example:

例如:

export default function withRequiredAuthProps<T>(model: T) {
  return {...model, connection: 'Username-Password-Authentication'};
}

Then your Http service that sends the model back to the service can use a type constraint to verify that the property has been added before making the request

然后将模型发送回服务的 Http 服务可以使用类型约束来验证在发出请求之前是否已添加该属性

For example:

例如:

export default class MyHttpClient {
  post<T extends {connection: 'Username-Password-Authentication'}>(url: string, body: T) {
    //...
  }
}

These are just meant as examples but they are based on code that is minimal, and that works.

这些只是作为示例,但它们基于最少且有效的代码。

回答by jarodsmk

just came across your question & was investigating the same thing.

刚刚遇到您的问题并正在调查同样的事情。

With reference to the Angular 2 Style docs here, the 'guideline' is to NOT implement an interface on the premise that a class alone can:

参考此处Angular 2 Style 文档,“准则”是不实现接口的前提是单独的类可以:

  • Act as an interface (if you use the implements keyword instead of the extends keyword)
  • Be smaller than a class-implementing-interface
  • 充当接口(如果您使用 implements 关键字而不是 extends 关键字)
  • 比类实现接口小

As a rule of thumb:

根据经验:

Angular Guidelines> Typescript Guidelines(in the case of Angular 2 projects).

Angular 指南> Typescript 指南(在 Angular 2 项目的情况下)。

Hope this helps you out! :)

希望这可以帮助你!:)