简体   繁体   English

如何在httpClient / Observable / Map或其他内容中转换为Angular Model变量?

[英]How to cast to Angular Model variables in httpClient/Observable/Map or whatever?

because I'm prety new in Angular/Typescript I still have some fundamental questions, but couldn't find an appropriate answer. 因为我是Angular / Typescript的新手,所以我仍然有一些基本的问题,但是找不到合适的答案。 How can I receive or cast the right value types? 如何接收或转换正确的值类型?

I have defined a simple model: 我定义了一个简单的模型:

export class Message {
    ID:                 number;
    mode:               string;
    senderID:           number;
    receiverID:         number;
    marked:             boolean;
    subject:            string;
}

Now I'm getting data from the server, via a message.service like: 现在,我通过message.service从服务器获取数据,例如:

getMessages(): Observable<Message[]> {
    return this.restService.get('/message/list/' + this.owner)
}

and in my component I read the values, via: 在我的组件中,我通过以下方式读取了值:

this.messageService.getMessages().subscribe((messages:Message[]) => {
    this.objectlist = messages
}

What I need and also expected are the real data types of the message elements, but everything is of String . 我需要的也是消息元素的实际数据类型,但是一切都是String的 Why there are no booleans and numbers ? 为什么没有布尔值数字

I also played with pipe() and map(), but I'm not able to make a type conversion. 我也玩过pipe()和map(),但无法进行类型转换。

What I'm doing wrong? 我做错了什么? I hope that somebody can give me a helping hand. 我希望有人能帮助我。 Thx in advance. 提前谢谢。

TypeScript is just a typing layer of javascript. TypeScript只是javascript的键入层。 So you cannot magically cast one value into the other just by defining the typings. 因此,您不能仅通过定义类型就将一个值神奇地转换为另一个值。 You really have to hardcode the types. 您确实必须对类型进行硬编码。 In your case: 在您的情况下:

getMessages(): Observable<Message[]> {
  return this.restService.get('/message/list/' + this.owner).pipe(
    map((messages) => messages.map((message) => ({
      ...new Message(), ...message, ...{
        ID: parseInt(message.ID),
        senderID: parseInt(message.senderID),
        receiverID: parseInt(message.receiverID),
        marked: message.marked === '1'
      }
    }))
  )
}

This makes sure the returned object is really an array of the Message class. 这样可以确保返回的对象确实是Message类的数组。 You can also think about using special decorators, which makes sure that when you create the class you get the proper type: 您还可以考虑使用特殊的装饰器,以确保在创建类时获得正确的类型:

export class Message {
    @IsNumber()
    ID:                 number;
    mode:               string;
    @IsNumber()
    senderID:           number;
    @IsNumber()
    receiverID:         number;
    @IsBoolean()
    marked:             boolean;
    subject:            string;
}

There are some third party libraries you can use for this, or just implement them yourself. 您可以使用一些第三方库,也可以自己实现。

You can then change the above code to: 然后,您可以将上面的代码更改为:

messages.map((message) => ({...new Message(), ...message))

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

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