简体   繁体   English

Angular - 如何处理后端到前端的命名约定?

[英]Angular - How to deal with Backend to Frontend naming convention?

I am using Angular as front end and Postgresql as database.我使用 Angular 作为前端和 Postgresql 作为数据库。

The interface below are names of columns in database which are being consumed by service下面的接口是数据库中正在被服务使用的列的名称

export interface AmazonDataLog {  
    customer_id: number;
    first_target: string;
    last_target: string;
   
}

Below is the ts file where processIncomingRecord function will get data and then render on UI下面是 ts 文件,其中processIncomingRecord function 将获取数据然后在 UI 上呈现

component.ts file组件.ts 文件

  firstLogs: AmazonDataLog | undefined = undefined;
 
  constructor(
    public userService: UserService,
    private records: AmmaService,
    private router: Router
  ){

    this.records.getNextRecord().subscribe(data => {
      this.processIncomingRecord(data);
    });

Below is html code where data is being displayed下面是正在显示数据的 html 代码

.html code .html代码

 <mat-divider></mat-divider>
 <mat-list-item class="bold">Amazon {{firstLogs.customer_id}} : {{firstLogs.first_target}}</mat-list-item>
 <mat-divider></mat-divider>
 <mat-divider></mat-divider>
 <mat-list-item class="bold">Alexa {{firstLogs.customer_id}} : {{firstLogs.last_target}}</mat-list-item>
 <mat-divider></mat-divider>
            

What I am looking for is to RENAME backend variable to CAMEL CASE and display on frontend我正在寻找的是将后端变量重RENAMECAMEL CASE并在前端显示

Eg - Backend service returns例如 - 后端服务返回

    customer_id: number;
    firstTarget: string;
    lastTarget: string;

Eg - On UI I want to display as CAMEL CASE例如 - 在 UI 上我想显示为CAMEL CASE

   {{customerID}} {{firstTarget}} and {{lastTarget}} 

How can I achieve this change?我怎样才能实现这种改变?

You cannot rename fields of an object when it is present.当 object 存在时,您不能重命名它的字段。 You have 2 options here.您在这里有 2 个选项。

1st第一

Define 3 optional fields in your interface在界面中定义 3 个可选字段

export interface AmazonDataLog {
    customer_id: number;
    first_target: string;
    last_target: string;
    customerID?: number;
    firstTarget?: string;
    lastTarget?: string;
}

And then you put a method in between where you just map the values.然后你在 map 值之间放置一个方法。

mapToCamelCase(data: AmazonLogData): void {
    data.customerID = data.customer_id;
    data.firstTarget = data.first_target;
    data.lastTarget = data.last_target;
}

The object gets processed "by reference" so you don't need to return it. object 被“通过引用”处理,因此您不需要返回它。

2nd第二

You build up a second interface with the names in CamelCase and then you do the mapping.您使用 CamelCase 中的名称构建第二个接口,然后进行映射。

export interface CcAmazonDataLog {
    customerID: number;
    firstTarget: string;
    lastTarget: string;
}


// You can remove the unnecessary fields afterwards, 
// But be sure that you never ever need to access them again later!
// the delete-lines are commented out (just as a precaution)
mapToCamelCase(data: AmazonLogData): CcAmazonLogData  {
   // instantiate an object ccdata using the interface CcAmazonLogData
   // [...]

    ccdata.customerID = data.customer_id;
    // delete data.customer_id;
    ccdata.firstTarget = data.first_target;
    // delete data.first_target;
    ccdata.lastTarget = data.last_target;
    // delete data.last_target;

    return ccdata;
}

And then do something like this然后做这样的事情

TS-File TS-文件

this.records.getNextRecord().subscribe(data => {
    this.mapToCamelCase(data);
    this.processIncomingRecord(data);
});

HTML HTML

 <mat-divider></mat-divider>
 <mat-list-item class="bold">Amazon {{firstLogs?.customerID}} : {{firstLogs?.firstTarget}}</mat-list-item>
 <mat-divider></mat-divider>
 <mat-divider></mat-divider>
 <mat-list-item class="bold">Alexa {{firstLogs?.customerID}} : {{firstLogs?.lastTarget}}</mat-list-item>
 <mat-divider></mat-divider>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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