navTrl.push() 中的承诺错误

promise error in navTrl.push()

我正在尝试构建一个简单的功能,让我可以在我的应用程序中浏览不同的页面。我希望能够在我的 html 代码中传递我想去的页面的名称。到目前为止我所做的是

在我的home.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

令我困惑的是承诺问题,因为变量是通过单击按钮传递的。难道只有当 var 未定义时才会出现 promise 问题吗?

您没有在代码的 click 事件中传递 ChooseCharacterPage

<button ion-fab color="light" (click)='goToPage("ChooseCharacterPage")'>

您实际上传递的是 字符串文字 ,这显然不是组件。 对于传递对象,您不会有任何内引号。 如果视图中不存在页面组件对象,则应直接在组件端导入并调用

this.navCtrl.push(ChooseCharacterPage)

在事件函数调用中不将其作为参数传递。 在你的函数中

gotoPage(page:string){
switch(page){
  case "ChooseCharacterPage": 
    this.navCtrl.push(ChooseCharacterPage);
    break;
  //other cases
  }
}

正如@suraj 所说,您需要向 push 方法发送一个组件(而不是字符串)。但也许更简单的方法是在你的 ts 代码中声明一个 属性,分配 ChooseCharacterPage 组件 class:

public chooseCharacterPage: any = ChooseCharacterPage;

// ...

goToPage(test: any){
    this.navCtrl.push(test);
}

然后在您看来您可以使用该变量:

<button ion-fab color="light" (click)='goToPage(chooseCharacterPage)'>

请注意,在 html 中我们现在使用 属性(名称以小写字母开头,并且不在引号之间)。