【问题标题】:Ionic 4 (Angular 7) - sharing components issueIonic 4 (Angular 7) - 共享组件问题
【发布时间】:2019-03-31 21:44:07
【问题描述】:

我正在尝试为像 Angular 这样的框架做一件非常平常的事情。目标是通过共享模块多次使用相同的 (HeaderComponent) 组件。

我的 shared.module.ts:

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { HeaderComponent } from '../header/header.component';
import { IonicModule} from '@ionic/angular';

@NgModule({
    imports: [
        CommonModule, 
        IonicModule
    ],
    declarations: [
        HeaderComponent
    ],
    exports: [
        HeaderComponent
    ]
})

export class SharedModule {}

在 app.module.ts 我添加了这个:

imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, SharedModule],

在 home.page.html 中我尝试以这种方式呈现它:

<app-header></app-header>

它确实有效,因为浏览器向我显示了诸如

之类的错误

'ion-col' 不是已知元素

HeaderComponent 中的所有离子组件等等。

我在 Internet 上找到了解决该问题的方法,建议将 IonicModule.forRoot(HeaderComponent) 添加到 shared.module.ts 的 imports 数组中,但是这种方法导致以下错误:

'app-header' 不是已知元素

好像它不再可用。

【问题讨论】:

  • 用这个 Ionic 4 把我的头发拉出来。如何在 Ionic 4 中使用共享组件.....!!!!!

标签: angular ionic4


【解决方案1】:

您还必须像这样将离子模块添加到您的共享模块中:

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { HeaderComponent } from '../header/header.component';
import { IonicModule } from 'ionic-angular';

@NgModule({
  imports: [
    CommonModule,
    IonicModule
  ],
  declarations: [
    HeaderComponent
  ],
  exports: [
    HeaderComponent
  ]
})

export class SharedModule {}

如果您使用 ionic 4,则必须将 IonicModule 的导入编辑为:

import { IonicModule } from '@ionic/angular';

【讨论】:

  • IonicModule 是在根级别添加的,所以不需要
  • 你错了。如果使用 ionic 生成共享组件模块,该模块默认导入 ionic 模块。如果你说的是真的,为什么即使你在root中导入浏览器模块,你还要导入commonmodule?
  • 是的,有道理:)
  • 终于想通了
【解决方案2】:

在我的情况下,问题是我试图在使用模式打开的组件中包含自定义组件。我需要将我的组件添加到父页面的模块中。

【讨论】:

    【解决方案3】:

    我在尝试创建第三个(即共享模块)时遇到了很多问题,所以我发现这很有帮助: https://forum.ionicframework.com/t/ionic-4-create-shared-component-up-to-date-example-please/176887

    我没有遵循关于摆脱东西的部分,但我确实向我的组件添加了一个模块(在同一个文件夹中)。然后在其他页面中,我能够在顶部导入这个模块,然后在导入数组中导入,这很有效。

    最终我不得不导入其他东西,比如 Gary 上面所说的,但这只是常识。

    我在网上找到的大多数教程都让你创建另一个组件,然后使用它附带的模块。遵循这种思路,我感到非常困惑。我认为它基本上归结为,您可以共享模块,但不能共享组件。不要引用我的话,但这就是我现在的理解。

    header.component.html

         <ion-header>
    ...
    
        </ion-header>
    

    header.component.ts

    import { Component, OnInit, Input } from '@angular/core';
    import { feedService } from "../services/feed.service";
    
    @Component({
      selector: 'app-header',
      templateUrl: './header.component.html',
      styleUrls: ['./header.component.scss'],
    })
    export class HeaderComponent implements OnInit {
     ...
      constructor() {}
    
      ngOnInit() {}
    }
    

    header.module.ts

    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { FormsModule } from '@angular/forms';
    import { IonicModule } from '@ionic/angular';
    import { HeaderComponent } from "./header.component";
    
    @NgModule({
        imports: [
          ...  
        ],
        declarations: [HeaderComponent],
        exports: [HeaderComponent],
      })
      export class HeaderModule{
      }
    

    最终在该页面的 module.ts 中的其他地方使用它

    import ... //other modules
    
    import { HeaderModule  } from "../header/header.module";  //your relative path
    
    @NgModule({
      imports: [
        //other modules
        HeaderModule,
      ],
      declarations: [],
      entryComponents: [],
    })
    export class MyPageModule {}
    

    然后在该页面的 html 文件中,我可以使用上面 header.component.ts 中声明的选择器。

    &lt;app-header ...&gt;&lt;/app-header&gt;

    【讨论】:

    • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2023-03-10
    • 2020-03-14
    • 2018-09-27
    • 2019-05-03
    • 2018-05-04
    • 2019-08-05
    相关资源
    最近更新 更多