【问题标题】:Nativescript and ListView, item undefinedNativescript 和 ListView,未定义项
【发布时间】:2016-12-16 12:24:58
【问题描述】:

我正在尝试使用 NativeScript 进行一些实验,但我遇到了一些奇怪的错误,我无法理解这一点。

我的组件中有一个简单的口袋妖怪列表:

import {Component} from '@angular/core';
import {Http} from '@angular/http';

@Component({
  selector:'pokemon-list',
  templateUrl: 'components/pokemonList/pokemonList.html'
})
export default class PokemonList {

  pokemons: Array<any>;

  constructor(private http:Http){
    this.pokemons = [
      {name: 'Bulbi', url: 'google.com'}
    ];
  }
}

并且关联了以下模板:

<StackLayout>
  <Label text="Pokemons" class="h1 text-center"></Label>
  <ListView [items]="pokemons">
    <template let-item="pokemon">
      <pokemon-item [name]="pokemon.name" [url]="pokemon.url"></pokemon-item>
    </template>
  </ListView>
</StackLayout>

当我尝试启动应用程序时,我的控制台中出现以下错误:

Error in components/pokemonList/pokemonList.html:4:20 caused by: undefined is not an object (evaluating 'self.parentView.context.pokemon.name')

我做错了什么? :/

【问题讨论】:

    标签: javascript angular typescript nativescript


    【解决方案1】:

    在 Angular-2 中,在 HTML 中声明 let 的语法如下:

    let-myName="item"
    

    在你的情况下:

    let-pokemon="item"
    

    起初它有点奇怪,但它完全有道理。 完整的例子请看here

    【讨论】:

    • 一开始听起来很奇怪,NativeScript 并没有多大帮助,因为它们的默认示例是let-item="item",这让我认为语法是let-item="myCustomName"。感谢您的澄清!
    猜你喜欢
    • 1970-01-01
    • 2019-04-07
    • 2020-02-28
    • 1970-01-01
    • 2022-01-03
    • 2017-04-24
    • 2018-09-20
    • 1970-01-01
    • 2019-09-27
    相关资源
    最近更新 更多