【发布时间】:2018-11-10 08:57:51
【问题描述】:
我不知道如何使用 Ionic 框架教程模板在 Angular 中访问组件的属性。它似乎未初始化,因为我试图将其打印到控制台。这是 app.component.ts 中的代码:
export class MyApp {
@ViewChild(Nav) nav: Nav;
pages: Array<{title: string, badge: number, component: any}>;
constructor(
public platform: Platform,
public menu: MenuController,
public statusBar: StatusBar,
public splashScreen: SplashScreen,
public authService: AuthService,
public loadingCtrl: LoadingController,
private toastCtrl: ToastController
) {
this.initializeApp();
// set our app's pages
this.pages = [
{ title: 'Series', badge: 0, component: SeriesDetailPage },
{ title: 'Borrow', badge: 0, component: BorrowViewPage },
{ title: 'Request', badge: 0, component: RequestViewPage },
{ title: 'Borrow List', badge: 0, component: BorrowListPage },
{ title: 'Request List', badge: 0, component: RequestListPage },
{ title: 'Setting', badge: 0, component: SettingPage }
];
var borrowPage = this.pages.find(page => page.title === 'Borrow');
console.log((borrowPage.component as BorrowViewPage).items); //Print undefined
this.nav.setRoot(BorrowViewPage);
}
...
openPage(page) {
// close the menu when clicking a link from the menu
this.menu.close();
// navigate to the new page if it is not the current page
this.nav.setRoot(page.component);
}
这是 BorrowViewPage 组件的代码:
export class BorrowViewPage {
public items: Borrow[];
constructor(public navCtrl: NavController, public authService: AuthService, public navParams: NavParams) {
this.view(); //Make a HTTP call to initialize items array
}
}
此时似乎没有任何组件调用其构造函数。如果可以,是否可以手动调用组件构造函数?
添加: 神奇的是我什至不知道类实例来自哪里。这是应用从 app.module.ts 开始的地方:
@NgModule({
declarations: [
MyApp,
SettingPage,
SeriesDetailPage,
SeriesListPage,
BorrowListPage,
BorrowViewPage,
RequestListPage,
RequestViewPage
],
imports: [
BrowserModule,
HttpModule,
IonicModule.forRoot(MyApp),
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
SettingPage,
SeriesDetailPage,
SeriesListPage,
BorrowListPage,
BorrowViewPage,
RequestListPage,
RequestViewPage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
AuthService
]
})
export class AppModule {}
通过在 app.component.ts 中将根页面设置为您想要的任何页面,该组件名称的构造函数将被调用。我认为构造函数被调用了?!没有把握。当我单击 app.html 中指定的任何按钮时,它的工作方式相同:
<ion-menu [content]="content">
<ion-header>
<ion-toolbar>
<ion-title>MyApp</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<button ion-item *ngFor="let p of pages" (click)="openPage(p)">
{{p.title}}
<ng-container *ngIf="p.badge != 0">
<span class="badge badge-assertive">{{p.badge}}</span>
</ng-container>
</button>
</ion-list>
</ion-content>
</ion-menu>
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
在这种情况下,我什至如何检索该类实例?
【问题讨论】:
-
从您的代码中完全不清楚它想要的行为是什么。稍微简化一下,执行
{ component: SeriesDetailPage }您将获得 class 属性而不是 class instance - 这显然不是您所需要的。但是,除了您之外没有人知道从哪里获取该实例,因为它完全取决于您的代码的其余部分......最明显的方法是像这样做:{component: new SeriesDetailPage(...params...) }- 但考虑到构造函数参数建议 DI你不太可能想要这样。 -
实际上,我不知道该类实例来自哪里。这对我来说是个谜。它就是这样工作的。我只是不知道怎么做。让我发布 app.module.cs 的代码。也许会有所帮助。
标签: angular typescript ionic-framework