错误TypeError:“ _ co.client未定义”

时间:2019-11-21 12:35:44

标签: angular

我已经恢复了Client的数据,现在我想恢复成功执行的每个Client的详细信息,但这给了我这个错误。

我已经恢复了Client的数据,现在我想恢复成功执行的每个Client的详细信息,但这给了我这个错误。

  

错误TypeError:“ _ co.client未定义”

client.service.ts

       import { map } from 'rxjs/operators';
        import { AngularFirestore,AngularFirestoreCollection,AngularFirestoreDocument } from 'angularfire2/firestore';
        import { Injectable } from '@angular/core';
        import { Client } from '../models/client';
        import { Observable } from 'rxjs';
    @Injectable({
      providedIn: 'root'
    })
    export class ClientService {
      clientsCollection:AngularFirestoreCollection<Client>;
      clientsDoc:AngularFirestoreDocument<Client>;

      constructor(private afs : AngularFirestore) { 
      this.clientsCollection=  this.afs.collection('clients');
      }
    getClient(id:string):Observable<Client>{
        return this.clientsCollection.doc(id).valueChanges();
      }
}

details-client.component.ts

import { FlashMessagesService } from 'angular2-flash-messages';
import { ClientService } from './../../services/client.service';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Client } from 'src/app/models/client';


@Component({
  selector: 'details-client',
  templateUrl: './details-client.component.html',
  styleUrls: ['./details-client.component.css']
})
export class DetailsClientComponent implements OnInit {
  id:string;  
  client:Client; 

  constructor(
    private clientService:ClientService,
    private route:ActivatedRoute,
    private flashMessage:FlashMessagesService
  ) { } 

  ngOnInit() {
    this.id = this.route.snapshot.params.id;
    this.clientService.getClient(this.id).subscribe(client =>{
      this.client = client;
      console.log(this.client);
    })

  }  

}

details-client.component.html

<div class="row m-3">
    <div class="col-md-6">
        <a routerlink="/"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i>go to dashboard</a>
    </div>
    <div class="col-md-6">
        <div class="btn-group">
            <button class="btn btn-danger">
              <i class="fa fa-pencil" aria-hidden="true"></i>Edit
            </button>
            <button class="btn btn-dark">
                <i class="fa fa-trash" aria-hidden="true"></i>Delete
            </button>
        </div>
    </div>
</div>
<div class="card">
    <div class="card-body">
        <h4 class="card-title">{{ client.firstname }} {{ client.lastname }}</h4>

        <div class="row"> 
            <div class="col-md-8">
                <ul class="list-group">
                    <li class="list-group-item ">id : <strong>{{ id }}</strong></li>
                    <li class="list-group-item ">Phone : <strong>{{ client.phone }}</strong></li>
                    <li class="list-group-item">email : <strong>{{ client.email }}</strong></li>
                    <li class="list-group-item ">Disabled item</li>
                </ul>
            </div>
            <div class="col-md-4 text-right">
                balance  <button class="btn btn-success"><i class="fa fa-pencil" aria-hidden="true"></i></button>
                <h4>{{ client.balance | currency:"USD":"symbol"}}</h4>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:2)

您的问题出在模板中,因为您的客户端变量在开始时是未定义的,因为.subscribe是异步方法。

尝试使用管道 async (| async),例如:

*ngIf="client.id!=5 | async"
//OR
{{client.id | async}}

或者您可以在对象变卖之前使用,例如:

*ngIf="client?.id!=5 "
//OR
{{client?.id}}