简体   繁体   English

将HTTP响应json转换为Angular8中的typescript对象

[英]Converting HTTP response json into typescript Object in Angular8

I am trying to create a typescript object from an HTTP response in angular8, But I receive an error: 我试图从angular8中的HTTP响应创建一个typescript对象,但我收到一个错误:

ERROR ReferenceError: Profile is not defined at SafeSubscriber._next (app.service.ts:17) at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:196) at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:134) at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:77) at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54) at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:41) ERROR ReferenceError:SafeSubscriber._next(app.service.ts:17)未在SafeSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.SafeSubscriber .__ tryOrUnsub(Subscriber.js:196)中定义配置文件在Subscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber._next的SafeSubscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.SafeSubscriber.next(Subscriber.js:134) (Subscriber.js:77)在Subscriber.push ../ node_modules / rxjs / _esm5 / internal / Subscriber.js.Subscriber.next(Subscriber.js:54)在MapSubscriber.push ../ node_modules / rxjs / _esm5 / internal /operators/map.js.MapSubscriber._next(map.js:41)

My Profile.ts file is : here My app.service.ts file is: here 我的Profile.ts文件是: here我的app.service.ts文件是: here

I construct a component class object with this service result and I also get the error 我用这个服务结果构造一个组件类对象,我也得到了错误

ERROR TypeError: Cannot read property 'name' of undefined 错误TypeError:无法读取未定义的属性“name”

when I use it in the HTML file. 当我在HTML文件中使用它时。

The console output for the statement in service file is: 服务文件中语句的控制台输出是:

{
  name: "somename",
  status: "sometext",
  bio: "sometext",
  email: "email",
  gitlab: "link",
  twitter: "link",
  instagram: "link",
  linkedin: "link",
  telegram: "link"

},

This is output accompanied by the errors. 这是伴随错误的输出。

My HTML code shortly is : 我的HTML代码很快就会出现:

<p class="ui header">{{profile.name}}</p>

My component class file is: here 我的组件类文件是: here

The issue is in getProfile() method of your AppService . 问题出在AppService getProfile()方法中。 You are returning this.profile from this method, instead, you should return an observable like this: 你从这个方法返回this.profile ,相反,你应该返回一个这样的observable:

getProfile(): Observable<Profile> {
    return this.http.get(this.profileApiUrl) 
               .pipe(
                  map(pro => new Profile(pro))
               );
  }

Now in component inject this service in the component constructor like this: 现在在组件中将此服务注入组件构造函数中,如下所示:

//have a class variable
//this will be used in the template to render the profile
profile: Profile;
constructor(private _appService: AppService) {}

ngOnInit() {
 this._appService.getProfile().subscribe(pro => {this.profile = pro;});
}

Use a safe operator in the template - 在模板中使用安全操作符 -

<p class="ui header">{{profile?.name}}</p>

You don't need extra code to map to Profile object. 您不需要额外的代码来映​​射到Profile对象。 HttpClient would do it for you. HttpClient会为你做的。 You need to make few changes to service.ts as below: 您需要对service.ts进行一些更改,如下所示:

getProfile(): Observable<Profile> {
    this.http.get<Profile>(this.profileApiUrl)
    .pipe(map((pro: any) =>{
      this.profile = pro;
      return pro
    });
  }
}

In Component: 在组件中:

ngOnInit() {
 this._appService.getProfile().subscribe(pro => this.profile = pro;);
}

And in you html: 在你的HTML:

<p class="ui header">{{profile?.name}}</p>

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

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