【问题标题】:Ionic 2 Modal not showingIonic 2 模态未显示
【发布时间】:2016-10-27 08:41:42
【问题描述】:

我已经按照Ionic2 modals 上给出的示例进行操作,并且没有收到任何错误,但是当我单击应该启动模式的卡片时,什么也没有发生。

这是模态本身的代码:

@Component ({
    template: `
        <ion-card class='popover'>
            <ion-card-content>
                Hello
            </ion-card-content>
        </ion-card>
    `
})

export class AccModal {

    dumbData: number;
    constructor() {
         console.log("constructor");
         this.dumbData= 22;
    }
}

我的模态将显示的页面如下所示:

<ion-card (click)='presentModal()' class='custom-card'>
    <ion-card-header>
        Sched. Accuracy
    </ion-card-header>
    <ion-card-content>
        71%
    </ion-card-content>
</ion-card>

使用这样的打字稿:

presentModal() {
    let myModal = Modal.create(AccModal, {param: "something"});
    console.log('myModal is ', myModal);
    this.nav.present(myModal);
    console.log("function being called");
}

presentModal 中的 console.log 正在注册,但模态构造函数中的 console.log 未注册。我不知道该怎么办,因为我不能 100% 确定发生了什么?

更新

当我调试到 nav.present(导航控制器功能)时,我看到的是:

if (rootNav['_tabs']) {
    // TODO: must have until this goes in
    // https://github.com/angular/angular/issues/5481
    void 0;
    return;
}

我的项目中确实有选项卡,因此该语句的计算结果为 true,并且当前函数有效地返回一个 0 并称其为好。在我的 package.json 中,我的离子版本是:"ionic-angular": "^2.0.0-beta.8", "ionic-native": "^1.1.0"

希望这些附加信息有助于为比我更聪明的人进行诊断。

更新 2

我已更新到 2.0.0-beta.9 中的最新 ionic 2 版本。但是,当我在 chrome 控制台中调试时,我仍然在 ionic-angular 代码中的 nav.present 函数中看到上面的代码,即使当我在自己的代码中查看它时,我也会看到:

if (rootNav['_tabs']) {
  // TODO: must have until this goes in
  // https://github.com/angular/angular/issues/5481
  console.error('A parent <ion-nav> is required for ActionSheet/Alert/Modal/Loading');
  return;
}

我已清除缓存并重新加载页面,但旧代码仍然显示。我一定是疯了。对此的任何见解都会令人惊叹。

更新 3

这是我的标签的代码。 app.html 中的 live 和 index 变量只是在右侧选项卡上启动应用程序的一种方式。它以 1(或第二个选项卡)开头:

<ion-tabs greenTheme [selectedIndex]="index">
    <ion-tab tabIcon="people" #content tabTitle="My Roster" [root]="tab2"></ion-tab>
    <ion-tab tabIcon="stats" #content tabTitle="Wage Overview" [root]="tab1"></ion-tab>
</ion-tabs>

【问题讨论】:

  • 你的 this.nav 是你的导航控制器吗?
  • 是的。对不起,我遗漏了构造函数
  • .present(myModal) 返回一个承诺。试试.present(myModal).then((res) =&gt; { debugger; }).catch((err) =&gt; { debugger;})看看这里有没有返回有用的信息

标签: angular typescript ionic2 ionic3


【解决方案1】:

像这样在构造函数中导入 NavController:

constructor(private navController: NavController) {
  // Whatever goes here
}

你的方法调用this.navController.present(myModal);

【讨论】:

  • 对不起。我在评论中的意思是我正在导入导航控制器,就像你在这里一样(除了我的只是称为导航)我只是认为没有必要在我的问题中添加到代码中。
【解决方案2】:

我对处理选项卡的代码一无所知,但我用这篇文章中的代码创建了一个 Plunker(以及一个非常小的更改),并且模态显示正确。

我所做的更改,在模态代码中:

// Add NavParams to use it later in the modal's constructor
import { ..., NavParams } from 'ionic-angular';

@Component ({
    template: `
        <ion-card class='popover'>
            <ion-card-content>
                Hello
            </ion-card-content>
        </ion-card>
    `
})
export class AccModal {

  private dumbData: number;

  // I've added the params variable, because you're calling this constructor
  // with a parameter (called 'param' and with the 'something' value)
  constructor(private params: NavParams) {

   console.log("constructor");
   this.dumbData= 22;

   // You can see in the console the parameter being printed properly
   console.log('Param:', params.get('param'));
 }

}

您可以使用this plunker (Ionic2 beta.9)。

======================================

更新:

我已更新 plunker 以包含 tab 布局,并且它按预期工作。

app.html 中的直播...

我试图让它工作,包括app.html 中的标签,但做不到。所以,我在home page 中添加了标签,它只包含两个标签,在app.ts 中我只是设置了this.rootPage = HomePage;,所以这是应用程序的第一页。您能否尝试在您的应用程序中执行此操作,看看是否包含 app 页面中的选项卡可以解决问题?

【讨论】:

  • 我认为标签问题是问题的症结所在,因为如上面的代码所示,当前函数首先检查导航是否有标签,如果有,则返回 0,实际上不会渲染我的模态的。
  • 是否有一种简单的方法可以复制与该 plunker 中的选项卡相关的代码?这样我就可以对其进行调试并仔细查看内部发生的情况。
  • 我添加了我的标签页 html。在 app.js 中,我将 tab1 和 tab2 变量分配给用作自己页面的实际组件。
  • 我已经更新了 plunker。请告诉我这是否可以解决问题或有帮助。
  • 抱歉这么久没有回复。您可以做些什么来更改您的 plunker,使其更能代表我的代码在您的 app.ts 文件中,将模板从 &lt;ion-nav [root]="rootPage"&gt;&lt;/ion-nav&gt; 更改为 &lt;ion-tabs [selectedIndex]="0"&gt; &lt;ion-tab tabIcon="people" #content tabTitle="My Roster" [root]="rootPage"&gt;&lt;/ion-tab&gt; &lt;ion-tab tabIcon="stats" #content tabTitle="Wage Overview" [root]="tab1"&gt;&lt;/ion-tab&gt; &lt;/ion-tabs&gt;
【解决方案3】:

在 ionic 2 beta 11 中,更新修复了该问题。我的代码与示例here on ionic's site 的唯一区别是我的模态模板。超级简单,并且直接说明它是如何完成的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2018-01-04
    • 2018-06-11
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多