【问题标题】:Ionic 4 custom componentsIonic 4 自定义组件
【发布时间】:2019-04-10 16:08:23
【问题描述】:

在运行ionic g component myComponent 命令后,如何在 ionic 4 中加载组件?我想将新生成的自定义组件添加到我的主页。

【问题讨论】:

  • 我实际上有完全相同的问题,它也很容易重现。刚开始一个新项目:ionic start project blank --type=angular。然后生成一个组件: ionic g component timer 最后添加 到 home.page.html 会报“app-timer is not a known element”的错误。我已经阅读了很多 v4 文档,但无济于事。我意识到这并不能解决它,但我想我会提供一个重现。编辑:缺少换行符,对此感到抱歉。

标签: angular typescript ionic-framework ionic4


【解决方案1】:

终于想通了,这是一个可行的复制:

ionic start myProject blank --type=angular
ionic g module components
ionic g component components/myComponent --export

这会为“myComponent”添加声明和导出到组件模块。

要使用该组件,只需将ComponentsModule 添加到页面模块中的imports,例如

@NgModule({
imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    ComponentsModule,
    RouterModule.forChild([
        {
            path: '',
            component: HomePage
        }
    ])
],
declarations: [HomePage]
})
export class HomePageModule { }

这样就不会向app.module.ts 添加任何内容。

还要注意如果你想在自己的自定义组件中使用任何组件,你需要在components.module.tsimports 中添加IonicModule

希望这会有所帮助:-)

【讨论】:

  • 不知何故,它适用于一个空白的新项目,但在我现有的项目中,它加载了一个没有错误报告的空页面。
  • 非常感谢。我发现这很有帮助
  • 如果我想在任何页面中使用单个组件..?那我还需要将整个 componentModule 导入我的页面模块吗?
  • 由于某种原因,--export 标志对我不起作用,但手动将 MyComponentComponent 添加到 ComponentsModule 声明和导出数组。
  • 使用 glmdev 的评论它可以工作,但在 html 中组件显示“找不到元素”。在我将 myComponent 添加到 home.module.ts 中的 EntryComponents 后,它对我有用。
【解决方案2】:

基本上,ionic g component myComponent 会更新 app.module.ts 并在 app 文件夹中创建组件。

但是,如果您想要一种更优雅的方式来添加组件。步骤如下:

ionic g module components

将生成一个名为components 的模块文件夹。然后生成一堆组件:

ionic g component components/myComponent --export
ionic g component components/myComponent2 --export
ionic g component components/myComponent3 --export
ionic g component components/myComponent4 --export

components.module.ts 内部可以这样写:

...
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';

import { MyComponentComponent } from './my-component/my-component.component';
import { MyComponentComponent2 } from './my-component2/my-component2.component';
import { MyComponentComponent3 } from './my-component3/my-component3.component';
import { MyComponentComponent4 } from './my-component4/my-component4.component';

@NgModule({
  declarations: [
    MyComponentComponent,
    MyComponentComponent2,
    MyComponentComponent3,
    MyComponentComponent4
  ],
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
  ],
  exports: [
    MyComponentComponent,
    MyComponentComponent2,
    MyComponentComponent3,
    MyComponentComponent4
  ]
})
export class ComponentsModule {}

然后确保将组件模块导入app.module.ts

...
import { ComponentsModule } from './components/components.module';
...

@NgModule({
  declarations: [AppComponent],
  imports: [
    ...
    ComponentsModule,
    ...
  ],
  providers: [
    ...
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

要测试组件,您需要重新创建一个页面或组件。

ionic g page testing

将组件模块导入您的测试组件/页面或(类似地导入您当前的主页):

...
import { ComponentsModule } from '../components/components.module';
...

@NgModule({
  imports: [
     ...
     ComponentsModule,
     ...
  ],
  declarations: [TestingPage]
})
export class TestingPageModule {}

最后,只需使用组件选择器在测试页面中编写组件即可。例如

<app-my-component></app-my-component>
<app-my-component2></app-my-component2>
<app-my-component3></app-my-component3>
<app-my-component4></app-my-component4>

希望这可能会有所帮助。

【讨论】:

  • 非常棒的解释。谢谢!
【解决方案3】:

这是你在 components>foot-card>foot-card.ts 中的选择器:

import { Component } from '@angular/core';

@Component({
  selector: 'foot-card',
  templateUrl: 'foot-card.html'
})
export class FootCardComponent {

  text: string;

  constructor() {
    console.log('Hello FootCardComponent Component');
    this.text = 'Hello World';
  }

}

这是你的 components.module.ts:

import { NgModule } from '@angular/core';
import { FootCardComponent } from './foot-card/foot-card';
import { IonicModule } from 'ionic-angular';

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

export class ComponentsModule {}

这是你的 app.module.ts:

import { FootCardComponent } from '../components/foot-card/foot-card';
import { ComponentsModule } from '../components/components.module'

@NgModule({
  declarations: [

  ],
  imports: [
    ComponentsModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    FootCardComponent
  ],
  providers: [
  ]
})
export class AppModule {}

您必须在 import 中导入组件模块,并在 app.module.ts 中的入口组件中声明组件名称。

在 components.module.ts 中,你必须声明和导出组件,但不要忘记导入 IonicModule。

我遇到了同样的问题,但没有人告诉我在 Components.module.ts 和 app.module.ts 中导入 IonicModule,只添加到 entryComponent 并导入 componentModule。

【讨论】:

    【解决方案4】:

    做完以上...

    仅适用于 home.page.html 在我的组件名称前添加 app,例如:

    <app-my-component></app-my-component>
    

    【讨论】:

      【解决方案5】:

      关键是要包含一个新模块,专门用于您的自定义组件。

      我只是不明白为什么命令“ionic generate component components/myComponent --export”不再创建这个过时的模块。我经历了同样的problem here

      【讨论】:

        【解决方案6】:

        在 Ionic 6.11.0 上测试此解决方案。完美运行。 创建名为“FeaturedProduct”的可重用组件的流程 -

        第一步:创建组件ionic g component components/FeaturedProduct

        第 2 步:为此组件创建一个模块。 ionic g module components/FeaturedProduct

        第 3 步:将 FeaturedProductComponent 导入到 features-product.module.ts 文件中

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

        第 4 步:在所需页面的 module.ts 文件中导入 FeaturedProductModule。

        @NgModule({
          imports: [
            ...
            ...
            FeaturedProductModule
          ],
          declarations: [Tab2Page]
        })
        

        现在组件可以被这个标签使用 &lt;app-featured-product&gt;&lt;/app-featured-product&gt;

        【讨论】:

          【解决方案7】:

          在您的 src/components 文件夹中

          myComponent.html

          //Here your component HTML Code. For example:
          <ion-list radio-group (ionSelect)="change($event, item.type);" 
            ... 
          </ion-list>
          

          components.module.ts

          import {myComponentComponent} from "./myComponent/myComponent";
          @NGModule({
            declatations: [myComponentComponent],
            imports:[],
            exports: [myComponentComponent]
          })
          export class ComponentsModule{}
          

          在您的 src/app 文件夹中

          app.module.ts

          import { MyComponentComponent } from "../components/myComponent/myComponent";
          
          @NGModule({
             declarations: [
                yourPages....,
               MyComponentComponent
             ]
          )}
          

          使用它:

          例如在 HomePage.html 中:

          <myComponent> </myComponent>
          

          【讨论】:

          • 'mycomponent' 不是已知元素: 1. 如果 'mycomponent' 是 Angular 组件,则验证它是该模块的一部分。 2. 要允许任何元素添加“NO_ERRORS_SCHEMA”到该组件的“@NgModule.schemas”。
          • 我忘记了 components.module.ts
          • 我猜你将不得不将 app.module.ts 声明编辑为
          • 是的,就像我的回答一样。在那里我描述了在 app.module.ts 中做什么
          • 不,请注意,在 ionic 4 中尝试这样做很困难
          【解决方案8】:

          只需输入以下命令即可生成自定义组件

          离子 g 成分 components/Component-Name

          将组件添加到 html

          【讨论】:

            猜你喜欢
            • 2020-01-27
            • 2019-11-27
            • 2019-12-17
            • 2019-12-29
            • 1970-01-01
            • 2019-07-26
            • 2017-09-16
            • 2019-12-19
            • 2019-10-29
            相关资源
            最近更新 更多