javascript - navTrl.push() 中的 promise 错误

标签 javascript angular typescript ionic2 ionic3

我正在尝试构建一个简单的函数,让我可以在应用程序中浏览不同的页面。我希望能够在 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/

相关文章:

javascript - 下面是提取一些十六进制数据、解释它并将其打印到 console.log 的代码。它在 'req' 循环中工作,但在外部不工作。为什么?

typescript - 以 Angular 2 扩展 routerLink?

angular - AG-GRID 无法将图标按钮添加到我的 ag-grid 中的 ag-grid 和多选复选框的每一行

javascript - 如何模拟使用在 Node 模块中导出的导入(ES6 typescript)进行单元测试的外部注入(inject)库

typescript :尽管在构造函数中初始化了变量值未定义

javascript - 找不到模块 'path'

javascript - 具有嵌套范围的 Angular,尝试使用 $parent 但 watch 仍然没有更新

Javascript 提升

javascript - 这段代码不起作用&他们是一种比使用 else if 语句更有效的方法吗?

angular - 使用 Angular 形式执行计算