簡體   English   中英

類型“ {}”上不存在屬性“ listName”

[英]Property 'listName' does not exist on type '{}'

當我在詳細信息頁面中運行以下代碼時,得到的屬性“ listName”在類型“ {}”上不存在。 看起來有些瑣碎,但整個下午我都無法理解這里出了什么問題。

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import {ListDetailsService} from '../../providers/list-details-service';


@Component({
  selector: 'page-item-details',
  templateUrl: 'item-details.html'
})
export class ItemDetailsPage {
  selectedItem: any;
  detailsItems : Array<{title: string, note: string, id: string}>;

  constructor(public navCtrl: NavController, public navParams: NavParams, public listDetailsService : ListDetailsService) {
    // If we navigated to this page, we will have an item available as a nav param
    this.selectedItem = navParams.get('item');

    this.detailsItems = [];
    this.listDetailsService.load(this.selectedItem.id).then(myListItems => {
      console.log(myListItems);
      console.log(myListItems.listName);


    }).catch(function(e){
      console.log(e);
    })
  }
}

當我運行console.log(myListItems)時,我得到以下數據。

{
items: 
   [ { _id: 58bbf7fd463667f51d7804f6,
       votes: '10',
       item: 'Apple Iphone' },
     { _id: 58bbf7fd463667f51d7804f5,
       votes: '2',
       item: 'Google Phone' },
     { _id: 58bbf7fd463667f51d7804f4,
       votes: '2',
       item: 'Samsung Phone' },
     { _id: 58bbf7fd463667f51d7804f3, votes: '6', item: 'LG Phone' } ],
  __v: 0,
  listName: 'Best Phones',
  _id: 58bbf7fd463667f51d7804f2 }

但是,當我跑步時

console.log(myListItems.listName);

我懂了

Property 'listName' does not exist on type '{}'.

即使該屬性存在於對象myListNames中

我確定我在這里缺少一些瑣碎的東西。 但是我花了整個下午,不知道答案。

非常感謝您的幫助!

這是TypeScript編譯器錯誤,您應該將listDetailsService.load函數的返回類型更改為與列表對象匹配的any或定義的接口。

export class ListDetailsService {

  //...
  load(): any {
      //...
  }  

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM