繁体   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