简体   繁体   English

Ionic 4 (Angular 7) - 共享组件问题

[英]Ionic 4 (Angular 7) - sharing components issue

I'm trying to do an extremely usual thing for such a framework like Angular.我正在尝试为像 Angular 这样的框架做一件非常平常的事情。 The goal is to use the same ( HeaderComponent ) component more than once through a shared module.目标是通过共享模块多次使用相同的 ( HeaderComponent ) 组件。

My shared.module.ts:我的 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 {}

In app.module.ts I added this:在 app.module.ts 我添加了这个:

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

And in home.page.html I tried to render it this way:在 home.page.html 中,我尝试以这种方式呈现它:

<app-header></app-header>

It actually worked since browser showed me errors like它实际上有效,因为浏览器向我显示了类似的错误

'ion-col' is not a known element 'ion-col' 不是已知元素

and so on for all the ionic components from the HeaderComponent .对于HeaderComponent 中的所有离子组件,依此类推。

I've found a solution for the issue over the Internet that suggest adding IonicModule.forRoot(HeaderComponent) to the imports array of the shared.module.ts , but this approach causes the following error:我已经找到了问题在该建议增加互联网的解决方案IonicModule.forRoot(HeaderComponent)shared.module.ts进口阵列,但这种方法会导致以下错误:

'app-header' is not a known element 'app-header' 不是已知元素

As if it is no longer available.好像已经不能用了。

you additionally have to add the ionic module to your shared module like this:您还必须像这样将 ionic 模块添加到您的共享模块中:

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 {}

if you are using ionic 4 you have to edit the import of IonicModule to this:如果您使用 ionic 4,则必须将 IonicModule 的导入编辑为:

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

In my case the issue was that I was trying to include a custom component in a component that is opened with a modal.在我的情况下,问题是我试图在一个用模态打开的组件中包含一个自定义组件。 I needed to add my component to the parent page's module.我需要将我的组件添加到父页面的模块中。

I had lot issues with trying to create a 3rd, ie shared module, so I found this to be very helpful: https://forum.ionicframework.com/t/ionic-4-create-shared-component-up-to-date-example-please/176887我在尝试创建第三个(即共享模块)时遇到了很多问题,所以我发现这非常有帮助: https : //forum.ionicframework.com/t/ionic-4-create-shared-component-up-to-请日期示例/176887

I didn't follow the part about getting rid stuff but I did add a module to my component (in the same folder).我没有遵循关于摆脱东西的部分,但我确实在我的组件中添加了一个模块(在同一文件夹中)。 Then in the other pages I was able to import this module on top then in the imports array and that worked.然后在其他页面中,我能够在顶部导入这个模块,然后在导入数组中,并且工作正常。

Eventually I had to import other things like what Gary said above but that is just common sense.最终我不得不导入其他东西,比如上面加里所说的,但这只是常识。

Most tutorials I found online have you to create yet another component then use the module that comes with that.我在网上找到的大多数教程都要求您创建另一个组件,然后使用该组件附带的模块。 I was super confused following that line of thinking.遵循这种思路,我感到非常困惑。 I think it basically boils down to, you can share modules, but not components.我认为它基本上归结为,您可以共享模块,但不能共享组件。 Don't quote me but that is how I understand it now.不要引用我,但这就是我现在的理解。

header.component.html头文件.component.html

     <ion-header>
...

    </ion-header>

header.component.ts 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头文件.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{
  }

finally to use it else where, in that page's module.ts最后在该页面的 module.ts 中的其他地方使用它

import ... //other modules

import { HeaderModule  } from "../header/header.module";  //your relative path

@NgModule({
  imports: [
    //other modules
    HeaderModule,
  ],
  declarations: [],
  entryComponents: [],
})
export class MyPageModule {}

then on the that page's html file, I can use the selector declared in header.component.ts above.然后在该页面的 html 文件中,我可以使用上面 header.component.ts 中声明的选择器。

<app-header ...></app-header>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM