Ionic TypeScript错误(Property'nav'在'HomePage'类型上不存在)
问题描述:
我沿着这个Ionic 2教程,遇到问题。问题在于TypeScript(请参见图片)。这里是video tutorial I followed。Ionic TypeScript错误(Property'nav'在'HomePage'类型上不存在)
这里是src/pages/home/home.html
:
<ion-header>
<ion-navbar primary *navbar>
<ion-title>
Tasker
</ion-title>
<ion-buttons end>
<button ion-button icon-only>
<ion-icon name="add"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item *ngIf="!task.length">
No Task Available
<p> Click <ion-icon name="add"> to add task</ion-icon></p>
</ion-item>
<ion-item-sliding *ngFor="#t of tasks">
<ion-item>
<ion-toggle></ion-toggle>
<ion-label>
<h2 [ngClass]="t.status">{{t.task}}</h2>
<p [ngClass]="t.priority">{{t.priority}}</p>
</ion-label>
</ion-item>
<ion-item-options>
<button primary><ion-icon name="clipboard"></ion-icon>Edit</button>
<button danger><ion-icon name="trash"></ion-icon>Delete</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
</ion-content>
而且src/pages/home/home.ts
发生错误!:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
static get parameters(){
return [[NavController]]
}
constructor(nav) {
this.nav = nav;
this.tasks = [
{task:'test1', priority:'low', status:'pending'},
{task:'test2', priority:'high', status:'done'},
{task:'test3', priority:'normal', status:'pending'}
]
}
}
答
有几个打字稿问题,我看到:
- 你不需要
static get parameters
功能在所有。 -
如果您注射
NavController
,你可以像这样指定它:constructor(private nav:NavController) { //this.nav = nav; This is not required if you have set access //specifier in constructor parameter // removed rest of code for brevity }
-
最后,如果你需要创建一个打字原稿类变量,你需要在类中声明。
export class HomePage { tasks:any[]=[] // contstructor and other code }
注意参考视频好像是用离子的更旧版本。我建议你找到最近的教程视频。
我尝试了你的建议,并发现另一个错误,说它不能读取home.html文件中的一些属性,说它们是不确定的 –
你必须调试..例如'task.length'可能应该是任务.length' –
解析器错误:位于[#t中的任务]中的第1列中的意外标记#ng:///AppModule/[email protected]:22,我现在应该做什么?对不起,如此有需要... –