【问题标题】:Angular 6 through json and get data based on idAngular 6 通过 json 并根据 id 获取数据
【发布时间】:2019-03-19 18:19:16
【问题描述】:

错误

ServiceDetailComponent.html:3 错误类型错误:无法读取属性 未定义的“服务名称”;在 Object.eval [作为 updateRenderer] (ServiceDetailComponent.html:3);在 Object.debugUpdateRenderer [as updateRenderer] (core.js:11080);在 checkAndUpdateView (core.js:10456);在 callViewAction (core.js:10692);在 execComponentViewsAction (core.js:10634);在 checkAndUpdateView (core.js:10457);在 callViewAction (core.js:10692);在 execEmbeddedViewsAction (core.js:10655);在 checkAndUpdateView (core.js:10452);在 callViewAction (core.js:10692);


服务:

import { IServices } from './../../services';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ServiceService {

  // private _url: string = 'https://api.myjson.com/bins/1d8suw';
  private _url: string = 'https://api.myjson.com/bins/1azcrc';


  constructor(private http: HttpClient) { }

  getServices(): Observable<IServices[]> {
    return this.http.get<IServices[]>(this._url);
  }


  getService(id: number) {
    return this.http.get<IServices[]>(this._url + id );
  }

}

组件

import { Component, OnInit } from '@angular/core';
import { ServiceService } from '../services/service.service';
import { ActivatedRoute, Router } from '@angular/router';
import { IServices } from 'src/app/services';

@Component({
  selector: 'app-service-detail',
  templateUrl: './service-detail.component.html',
  styleUrls: ['./service-detail.component.css']
})
export class ServiceDetailComponent implements OnInit {
  id: number;
  service: IServices[];
  constructor(private serviceService: ServiceService,
    private route: ActivatedRoute
    ) { }

  ngOnInit() {
    this.id = this.route.snapshot.params['id'];

    this.serviceService.getService(this.id)
      .subscribe(service => {
          this.service = service;
      });

  }

}

HTML 结果

<p>
  service-detail works!
</p>

{{ service.serviceName }}

【问题讨论】:

  • 为了给您一个很好的答案,如果您还没有看过How to Ask,它可能会对我们有所帮助。如果您可以提供minimal reproducible example,它可能也很有用。
  • service.serviceName 可能仅在您的服务中的 ajax 请求完成后才能工作。在此之前,这是 100% 确定它未定义

标签: angular typescript angular-ui-router


【解决方案1】:
  1. 你的名字到处都是。 service: IService[] --> 你需要一个服务列表,所以让它services: IService[]
  2. this.services 属性将是未定义的,直到您从服务器获得响应。如果你想在模板中使用它,你应该使用elvis 操作符——这是? 的一个花哨的名字,看起来像这样:service?.serviceName。这实质上是告诉 Angular 在 service 未定义时不要抛出错误,并在该值可用时重新呈现该值。
  3. 由于this.services 是一个数组,您需要使用*ngFor 结构指令循环遍历项目:

    <p *ngFor="let service of services"> {{service.serviceName}} </p>

如您所见 - 我没有在这里使用 elvis 运算符,因为除非 services 值被初始化,否则 *ngFor 不会呈现任何内容。

【讨论】:

  • 我想通过 id 获取详细服务,getService(id: number) { return this.http.get(this._url + id ); }
  • 那么你的返回类型是错误的&lt;IServices[]&gt; 表示IService 的列表。如果您要获得一项服务,它应该是IService。为什么又是复数呢? --> IServices[] 读起来就像您正在获得IServices 的列表 - 这是一个列表列表。如果你犯了这样的错别字,就会让你自己和其他人更难理解你的代码中发生的事情。
【解决方案2】:

您将收到 undefined 错误,因为您尚未在 ServiceDetailComponent 中初始化 service 属性。你在回调函数中初始化它,当服务器返回响应时,它会异步运行。

检查 null 或使用以下可选语法:

{{ service?.serviceName }}

【讨论】:

【解决方案3】:

服务:IServices[];您正在尝试访问“服务”数组的属性。 引用数组中的项,然后引用属性。

【讨论】:

  • 谢谢,我没明白你的意思!
  • 请尝试使用截断的代码更新您的答案,并解释它解决问题的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-02
  • 2020-10-26
  • 2019-12-09
  • 1970-01-01
  • 2014-12-16
  • 2013-01-21
相关资源
最近更新 更多