为什么使用 Typescript 的 Nativescript 中的列表视图会带来错误 self.parent.parent 而不是对象?
Why does List view in Nativescript with Typescript bring error self.parent.parent not an object?
Update: The issue was a lack of understanding the syntax.
And the following question was written with general syntax resulting
in an example that doesn't create an error. See answer for
explanation.
与此类似,但我遵循给出的答案并有一个新问题。
caused by: undefined is not an object (evaluating
'self.parent.parent.context.item.name')
为什么引用 self.parent.parent?而不是 parent.child?这是我的问题所在吗?
app.component.html
<TabView #tabview [selectedIndex]="tabindex" class="">
<DockLayout *tabItem="{title: 'Tab1'}">
<StackLayout dock='left'>
<ListView [items]="items">
<template let-item="item">
<StackLayout>
<Label [text]='item.name' textWrap="true"> </Label>
</StackLayout>
</template>
</ListView>
</StackLayout>
</DockLayout>
</TabView>
app.component.ts
import { Component, OnInit } from "@angular/core";
import { Page } from "ui/page"
import { ListView } from "ui/list-view"
import { Item } from "../item";
import { ItemService } from "../../services/item.service";
import dockModule = require("ui/layouts/dock-layout");
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
providers: [ItemService]
})
export class AppComponent implements OnInit{
public items: Item[];
constructor(private itemService: ItemService) {
};
getItems(): void {
this.itemService.getItems().then(items => this.items = items);
}
ngOnInit(): void {
this.getItems();
}
}
item.ts
export class Item {
id: number;
name: string;
}
item.service.ts
import { Injectable } from '@angular/core';
import { Item } from '../item';
import { ITEMS } from '../database/mock-items';
@Injectable()
export class ItemService {
getItems(): Promise<Item[]> {
return Promise.resolve(ITEMS);
}
}
对于遇到此问题或类似问题的任何人。
最后,也许其他人的直觉,但我认为每个人都使用 <template let-item="item">
作为他们的代码只是巧合。说实话,上面的代码可以工作,因为模板标签写对了。但我面临的问题是我试图用另一个关键字编写语法。 <template let-item="account">
。与使用 *ngFor "let account of accounts"
相比,这将给出
not an object
像我上面提到的错误。在提出问题时,我在不知不觉中进行了编辑以反映正确的语法。
Update: The issue was a lack of understanding the syntax. And the following question was written with general syntax resulting in an example that doesn't create an error. See answer for explanation.
与此类似
caused by: undefined is not an object (evaluating 'self.parent.parent.context.item.name')
为什么引用 self.parent.parent?而不是 parent.child?这是我的问题所在吗?
app.component.html
<TabView #tabview [selectedIndex]="tabindex" class="">
<DockLayout *tabItem="{title: 'Tab1'}">
<StackLayout dock='left'>
<ListView [items]="items">
<template let-item="item">
<StackLayout>
<Label [text]='item.name' textWrap="true"> </Label>
</StackLayout>
</template>
</ListView>
</StackLayout>
</DockLayout>
</TabView>
app.component.ts
import { Component, OnInit } from "@angular/core";
import { Page } from "ui/page"
import { ListView } from "ui/list-view"
import { Item } from "../item";
import { ItemService } from "../../services/item.service";
import dockModule = require("ui/layouts/dock-layout");
@Component({
selector: "my-app",
templateUrl: "./app.component.html",
providers: [ItemService]
})
export class AppComponent implements OnInit{
public items: Item[];
constructor(private itemService: ItemService) {
};
getItems(): void {
this.itemService.getItems().then(items => this.items = items);
}
ngOnInit(): void {
this.getItems();
}
}
item.ts
export class Item {
id: number;
name: string;
}
item.service.ts
import { Injectable } from '@angular/core';
import { Item } from '../item';
import { ITEMS } from '../database/mock-items';
@Injectable()
export class ItemService {
getItems(): Promise<Item[]> {
return Promise.resolve(ITEMS);
}
}
对于遇到此问题或类似问题的任何人。
最后,也许其他人的直觉,但我认为每个人都使用 <template let-item="item">
作为他们的代码只是巧合。说实话,上面的代码可以工作,因为模板标签写对了。但我面临的问题是我试图用另一个关键字编写语法。 <template let-item="account">
。与使用 *ngFor "let account of accounts"
相比,这将给出
not an object
像我上面提到的错误。在提出问题时,我在不知不觉中进行了编辑以反映正确的语法。