【问题标题】:*ng-if not working inside ion-modal in ionic 5*ng-if 不在 ionic 5 中的 ion-modal 内部工作
【发布时间】:2020-12-03 09:38:42
【问题描述】:

我正在使用 ionic 5 构建一个移动应用程序,当我尝试调用其中包含 *ng-If 的 ion-modal 时,我会收到此错误

Can't bind to 'ngIf' since it isn't a known property of 'ion-header'.

modal是comment.page.ts中的一个comment section,这里是comment.page.html的代码

<ion-header class="ion-no-border" *ngIf="!isLoading">
    <ion-toolbar>
        <ion-title class="centerAM">{{no_comm | shortNumber}} comment{{no_comm>1?'s':''}}</ion-title>
    </ion-toolbar>
</ion-header>

<ion-content>
....

这里是 comment.module.ts 的代码

import { NgModule } from '@angular/core';
import { IonicModule } from '@ionic/angular'; 
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { NgxEmojModule } from 'ngx-emoj';

import { CommentPageRoutingModule } from './comment-routing.module';
import { CommentPage } from './comment.page';
import { PipesModule } from '../../pipes/pipes.module';
 
@NgModule({
    imports: [
        CommonModule,
        NgxEmojModule,
        PipesModule,
        FormsModule,
        IonicModule,
        CommentPageRoutingModule
    ],
    schemas: [],
    declarations: [ CommentPage]    
})
export class CommentPageModule {}

这里是从 home.page.ts 调用 modal 的函数

async CommentModal(i, id) {
    const modal = await this.modalCtrl.create({
        component: CommentPage,
        componentProps:{id},
        swipeToClose: true,
        cssClass: 'comment-modal'
    });
    await modal.present();
    return 
}

如果我应该在 home.module.ts 或 app.module.ts 中添加comment.module.ts,当页面加载时,它会自动加载模态而不需要用户点击任何东西,我还删除了从路线页面,它没有工作,请问我做错了什么

【问题讨论】:

    标签: javascript angular ionic-framework


    【解决方案1】:

    您的模块很可能被延迟加载。在这种情况下,the docs 建议您应该在需要此模态的模块内部导入模态模块 (CommentPageModule)。

    换句话说,你需要:

    ...
    @NgModule({
        imports: [
          ...
          CommentPageModule, // <--- here
        ]
    ...
    export class YourMainModule {}
    

    否则,模态组件不会完全加载。

    引用自文档:

    在延迟加载模态时,需要注意的是,模态不会在打开时加载,而是在加载导入模态的模块时加载。

    【讨论】:

      【解决方案2】:

      我在 angular 10、Ionic 5 上遇到了同样的问题,而这个问题通过导入解决了

      app.modual.ts 中的模态页面喜欢

      import { ModalPage } from './modules/core/modal/modal/modal.page';
      
      
      
      @NgModule({
        declarations: [
         ...
          ModalPage
        ]
      

      【讨论】:

        【解决方案3】:

        只需添加将要使用的组件

        @NgModule({
            imports: [
                CommonModule,
                FormsModule,
                IonicModule,
                UpdatePageRoutingModule],
            declarations: [UpdatePage, DownloadModalComponent],
            entryComponents: [DownloadModalComponent]
        })
        export class UpdatePageModule {}

        在你的模块声明和 entryComponents 这样的模态中

        DownloadModalComponent 是一个在 UpdatePage 模态中使用的组件。

        【讨论】:

          【解决方案4】:

          您需要确保已将BrowserModule 导入到您的模块中。

          import {BrowserModule} from '@angular/platform-browser'; 
          ....
          @NgModule({
              imports: [
                BrowserModule,
              ]
          ....
          

          【讨论】:

          • 没有 commonModule,BrowserModule 在 app.module.ts 中只需要一次
          猜你喜欢
          • 2020-09-02
          • 1970-01-01
          • 1970-01-01
          • 2023-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-02
          • 2021-12-08
          相关资源
          最近更新 更多