错误是ERROR TypeError:无法读取属性' FirstName'未定义的

时间:2018-06-05 17:26:45

标签: angular

我正在学习角度。在这里,我将人口统计组件作为标签,它将用于个人组件。它显示我的错误为demographic.component.html:3 ERROR TypeError:无法读取属性' FirstName'未定义的 如果有人想要解决它,我已经将它上传到stackblitz链接上了 https://stackblitz.com/edit/angular-6uhfoz

person.json

{
    "Demographics" : { 
            "ProfileId":1,
            "FirstName":"vinit"
           }

}

demographic.component.ts

import { Component, OnInit } from '@angular/core';
import * as jwt from 'angular2-jwt/angular2-jwt';
import { CardModule } from 'primeng/card';
import { ScrollPanelModule } from 'primeng/scrollpanel';
import { PersonListService,Person} from './person-list.service';

@Component({ 
  moduleId: module.id,
  selector: 'sd-demographic',
  templateUrl: 'demographic.component.html',
  styleUrls: ['demographic.component.css']
})
export class DemographicComponent implements OnInit {
    errorMessage: string;
    persons: Person;
    constructor(public personListService:PersonListService){}
    ngOnInit() {
       // console.log(jwt.AuthConfig);
       this.getperson();
    }

    getperson(){

      this.personListService.getDemographic()
      .subscribe(
       resp => this.persons = resp.Persons,

        //resp => this.addresses = resp.Addresses,
        error => this.errorMessage = <any>error
      );
 }
}

demographic.component.html

 <ul>
        <li><b>Demographics</b></li>
        <li>FirstName:{{ persons.FirstName }}</li>
    </ul>

person.component.ts

import { Component, OnInit } from '@angular/core';
import * as jwt from 'angular2-jwt/angular2-jwt';
import { PersonListService} from './person-list.service';

/**
 * This class represents the lazy loaded PersonComponent.
 */
@Component({
  moduleId: module.id,
  selector: 'sd-person',
  templateUrl: 'person.component.html',
  styleUrls: ['person.component.css']
})
export class PersonComponent implements OnInit {
  /*errorMessage: string;

*/
constructor(public personListService:PersonListService){}
  ngOnInit() {
     // console.log(jwt.AuthConfig);
     //this.getperson();
  }


 }

人-list.service.ts

import { Injectable } from '@angular/core';
    import { HttpClient } from '@angular/common/http';
    import { Observable } from 'rxjs/Observable';
    // import 'rxjs/add/operator/do';  // for debugging
    export class Person{

      ProfileId:number;
      FirstName:string="";
    }

    }

    /**
     * This class provides the NameList service with methods to read names and add names.
     */
    @Injectable()
    export class PersonListService {

      /**
       * Creates a new NameListService with the injected HttpClient.
       * @param {HttpClient} http - The injected HttpClient.
       * @constructor
       */
      constructor(private http: HttpClient) {}

      /**
       * Returns an Observable for the HTTP GET request for the JSON resource.
       * @return {string[]} The Observable for the HTTP request.
       */
      getDemographic(): Observable<{Persons: Person}>{
        console.log("Inside the get service")
        return this.http.get('app/person/person.json')

                     // .do(data => console.log('server data:', data))  // debug
                        .catch(this.handleError);

      }

person.component.html

<p-tabView>
    <p-tabPanel header="Demographics">

        <sd-demographic></sd-demographic>

    </p-tabPanel>
</p-tabView>

1 个答案:

答案 0 :(得分:2)

 <ul>
        <li><b>Demographics</b></li>
        <li>FirstName:{{ persons.FirstName }}</li>
    </ul>

当角度初始化你的模板时,人物静止未定义,直到执行以下操作:

    getperson(){

      this.personListService.getDemographic()
      .subscribe(
       resp => this.persons = resp.Persons,

        //resp => this.addresses = resp.Addresses,
        error => this.errorMessage = <any>error
      );
 }

由于HTTP它的异步人员将被解析,直到get完成并且你的模板能够显示它。

您需要做的是在模板上使用safe operator或ngIf。

 <ul>
        <li><b>Demographics</b></li>
        <li>FirstName:{{ persons?.FirstName }}</li>
    </ul>

 <ul *ngIf="persons">
        <li><b>Demographics</b></li>
        <li>FirstName:{{ persons?.FirstName }}</li>
    </ul>