我正在尝试使用 ionic2 开发我的第一个应用程序。我开始做我的第一页,一切正常,然后我决定添加第二页,我遇到了一个问题。当我单击应该打开下一页的按钮时,它已加载,但出现以下错误:
运行时错误
./NewsHeadline 类 NewsHeadline - 内联模板:7:1 中的错误由以下原因引起:找不到新闻页的组件工厂
叠
错误:找不到新闻页的组件工厂...
Ionic Framework: 2.0.0
Ionic Native: 2.4.1
Ionic App Scripts: 1.0.0
Angular Core: 2.2.1
Angular Compiler CLI: 2.2.1
Node: 6.9.4
OS Platform: Linux 4.4
Navigator Platform: Win32
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
但我不明白我在哪里错过了NewsPage声明。
这是我的家.html
<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<h2 class="center">mY FIRST List</h2>
<ion-list>
<news-headline *ngFor="let notice of newsList" [news]="notice">
</news-headline>
</ion-list>
</ion-list>
</ion-content>
这是我的应用程序.模块.ts
import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';
import {NewsHeadline} from '../components/news/newsHeadline';
@NgModule({
declarations: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage,
NewsHeadline
],
imports: [
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage
],
providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}]
})
export class AppModule {}
而news-page.ts仅包含以下代码:
import { Component } from '@angular/core';
import { NavParams } from 'ionic-angular';
import {News} from '../../components/news/news';
@Component({
selector: 'page-news-page',
templateUrl: 'news-page.html'
})
export class NewsPage {
news: News;
constructor(navParams: NavParams) {
this.news = navParams.data;
}
}
所以,问题的核心在我的newsHeadline.ts文件中,其中包含以下代码:
import {Component, Input} from '@angular/core';
import {News} from './news';
import {NewsPage} from '../../pages/news-page/news-page';
import { NavController } from 'ionic-angular';
@Component({
selector: 'news-headline',
template: `
<ion-item>
<ion-thumbnail item-left>
<img src="{{news.imagePreviewUrl}}">
</ion-thumbnail>
<h2>{{news.title}}</h2>
<p>{{news.date | date: 'dd/MM/yyyy'}}</p>
<button clear item-right (click)="goToNews(news)">Leggi</button>
</ion-item>`
})
export class NewsHeadline {
@Input()
news: News;
constructor(public navCtrl: NavController) {}
goToNews(news: News) {
this.navCtrl.push(NewsPage, news);
}
}
将 NewsPage 添加到声明和 entryComponents。您需要对您创建的每个新页面执行此操作。
更新
NgModule 中的声明、提供程序和导入有什么区别