Angular 返回承诺中的自定义类型
我有一些JSON数组,如下所示,我从服务中获得了这些JSONAngular 返回承诺中的自定义类型,angular,typescript,Angular,Typescript,我有一些JSON数组,如下所示,我从服务中获得了这些JSON [{ "id": 3, "name": "Arun" }, { "id": 5, "name": "Barun" }, { "id": 1, "name": "Joy" }, { "id": 8, "name": "Suman" }] 现在,我做了一个类似下面的 class Person{ id:number name:string deseriali
[{
"id": 3,
"name": "Arun"
}, {
"id": 5,
"name": "Barun"
}, {
"id": 1,
"name": "Joy"
}, {
"id": 8,
"name": "Suman"
}]
现在,我做了一个类似下面的
class Person{
id:number
name:string
deserialize(input) {
this.id = input.id;
this.name = input.name
return this;
}
}
现在,我有了一个服务类,从中我得到了带有Promise的JSON(数组/字典),我将这个Promise发送给我的解析函数。下面是我的解析函数
private parse(response:Promise<{}>):Promise<{}>{
return new Promise((success,failure) =>{
response.then((result) => {
if (result instanceof Array || Array.isArray(result)){
let rs1 = result.map((val) => new Person().deserialize(val))
success(rs1);
}else{
success(new Person().deserialize(result))
}
},(err) => {
failure(err)
});
});
}
是否有任何方法可以自动推断响应为Person对象或Person数组依赖于响应?
我是新来的,任何帮助都将不胜感激
我正在使用最新版本的Typescript。首先,让我们看看您的服务:您正在创建一个承诺,以处理另一个承诺。那是在重复你自己 此外,您应该使用可观察的,这是升级的承诺 最后,每次都要创建一个新的Person对象。考虑使用静态方法反序列化。< /P> 这将为您提供:
导出函数解析(响应:Promise):可观察{
//Rx 6:承诺返回(响应)
可观察的返回。来自承诺(响应)
.map(结果=>{
if(数组的结果实例| | Array.isArray(结果)){
返回result.map((val)=>Person.deserialize(val))
}否则{
返回人。反序列化(结果);
}
})
}
出口类人员{
id:编号;
名称:字符串;
公共静态反序列化(输入:任意):Person{
const person=新的person();
person.id=input.id;
person.name=input.name;
返回人;
}
}
现在,您可以像这样使用两个函数:
ExpectingObjectOffperson(){
this.parse(servicePromise)//不管是什么
.订阅(personInstance=>{
控制台日志(personInstance);
console.log(Person的personInstance实例);
},error=>console.log(error));
}
期望Arrayofperson(){
this.parse(servicePromise)//不管是什么
.订阅(个人=>{
for(const personInstance of persons){
控制台日志(personInstance);
console.log(Person的personInstance实例);
}
},error=>console.log(error));
}
检查可观察性,它有许多特性,更适合角度测量。;)谢谢@Radonirina,我一定会查的。但现在我想实现这一点。我将尝试按照您的建议进行操作,但我仍然想知道,如果我编写console.log(personInstance.nam),编译器是否会抛出错误?@Gour可能与您的键入有关(您不键入任何内容,因此编译器和linter都可以在构建的不同步骤中抛出不同的错误)这是我理解的,但servicePromise仍然没有被推断为所需的类型。@Gour我甚至不知道它是什么,正如我的答案代码中所述。我打错了。。servicePromise-->personInstance
expectingObjectOfPerson(){
//get the promise from service class
let result = servicePromise//Promise from service class with JSON
//now call parse function
this.parse(result)
.then((response) => {
//I knew that response is an object of Person in run time.
let person = response as Person
//log name of this person
console.log(person.name) //no run time exception
//log name of this person
console.log(person.nam) //Error thrown by compiler
//now if I do this
console.log(response.name) //no run time exception
//if this
console.log(response.nam) //run time exception, also no error thrown from compiler
/* Main problem is on here.
* Is there any way that response could automatically inferred to Person */
}, (error) => {
Console.log(error);
})
}
expectingArrayOfPerson(){
//get the promise from service class
let result = servicePromise//Promise from service class with JSON
//now call parse function
this.parse(result)
.then((response) => {
//I knew that response is an object of Person in run time.
let persons = response as Person[]
//log name of this person
console.log(persons.map((val) => val.name)) //no run time exception
//log name of this person
console.log(persons.map((val) => val.name)) //Error thrown by compiler
//now if I do this
console.log(response.map((val) => val.name)) //no run time exception
//if this
console.log(persons.map((val) => val.name)) //run time exception , also no error thrown from compiler
/* Main problem is on here.
* Is there any way that response could automatically inferred to array of Person */
}, (error) => {
Console.log(error);
})
}