我正在尝试构建一个简单的函数,让我可以在应用程序中浏览不同的页面。我希望能够在 html 代码中传递我想要进入的页面的名称。到目前为止我所做的是
在我家.ts
`goToPage(test){
console.log('clicked! '+test);
this.navCtrl.push(test);
}`
在我的 home.html
<button ion-fab color="light" (click)='goToPage("ChooseCharacterPage")'>
一切都加载了,但是当我点击时我得到了
invalid page component: ChooseCharacterPage
convertToView @ nav-util.js:23
NavControllerBase.push @ nav-controller-base.js:54
HomePage.goToPage @ home.ts:19
View_HomePage0.handleEvent_13 @ /AppModule/HomePage/component.ngfactory.js:193
(anonymous) @ view.js:408
(anonymous) @ dom_renderer.js:276
t.invokeTask @ polyfills.js:3
onInvokeTask @ ng_zone.js:227
t.invokeTask @ polyfills.js:3
e.runTask @ polyfills.js:3
invoke @ polyfills.js:3
error_handler.js:47 EXCEPTION: Uncaught (in promise): false
ErrorHandler.handleError @ error_handler.js:47
IonicErrorHandler.handleError @ ionic-error-handler.js:56
next @ application_ref.js:272
schedulerFn @ async.js:82
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:74
NgZone.triggerError @ ng_zone.js:278
onHandleError @ ng_zone.js:257
t.handleError @ polyfills.js:3
e.runGuarded @ polyfills.js:3
r @ polyfills.js:3
i @ polyfills.js:3
invoke @ polyfills.js:3
error_handler.js:52 ORIGINAL STACKTRACE:
ErrorHandler.handleError @ error_handler.js:52
IonicErrorHandler.handleError @ ionic-error-handler.js:56
next @ application_ref.js:272
schedulerFn @ async.js:82
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:74
NgZone.triggerError @ ng_zone.js:278
onHandleError @ ng_zone.js:257
t.handleError @ polyfills.js:3
e.runGuarded @ polyfills.js:3
r @ polyfills.js:3
i @ polyfills.js:3
invoke @ polyfills.js:3
error_handler.js:53 Error: Uncaught (in promise): false
at s (polyfills.js:3)
at polyfills.js:3
at Object.ti.reject (nav-controller-base.js:187)
at NavControllerBase._queueTrns (nav-controller-base.js:197)
at NavControllerBase.push (nav-controller-base.js:52)
at HomePage.goToPage (home.ts:19)
at CompiledTemplate.proxyViewClass.View_HomePage0.handleEvent_13 (/AppModule/HomePage/component.ngfactory.js:193)
at CompiledTemplate.proxyViewClass.<anonymous> (view.js:408)
at HTMLButtonElement.<anonymous> (dom_renderer.js:276)
at t.invokeTask (polyfills.js:3)
ErrorHandler.handleError @ error_handler.js:53
IonicErrorHandler.handleError @ ionic-error-handler.js:56
next @ application_ref.js:272
schedulerFn @ async.js:82
SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
SafeSubscriber.next @ Subscriber.js:172
Subscriber._next @ Subscriber.js:125
Subscriber.next @ Subscriber.js:89
Subject.next @ Subject.js:55
EventEmitter.emit @ async.js:74
NgZone.triggerError @ ng_zone.js:278
onHandleError @ ng_zone.js:257
t.handleError @ polyfills.js:3
e.runGuarded @ polyfills.js:3
r @ polyfills.js:3
i @ polyfills.js:3
invoke @ polyfills.js:3
令我困惑的是 promise 问题,因为变量是通过单击按钮传递的。难道只有当 var 未定义时才会出现 promise 问题吗?
最佳答案
就像@suraj所说,您需要将组件发送到push方法(而不是字符串)。但也许更简单的方法是在 ts 代码中声明一个属性,分配 ChooseCharacterPage
组件类:
public chooseCharacterPage: any = ChooseCharacterPage;
// ...
goToPage(test: any){
this.navCtrl.push(test);
}
然后在您看来,您可以使用该变量:
<button ion-fab color="light" (click)='goToPage(chooseCharacterPage)'>
请注意,我们现在在 html 中使用该属性(名称以小写字母开头,并且不在引号之间)。
关于javascript - navTrl.push() 中的 promise 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42467830/