Ionic 4自定义组件

时间:2018-11-07 08:37:46

标签: angular typescript ionic-framework ionic4

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

8 个答案:

答案 0 :(得分:20)

最后弄清楚了,这是一个有效的复制程序:

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不会被添加任何内容。

还要注意,如果要在自己的自定义组件中使用ANY组件,则需要将IonicModule添加到imports中的components.module.ts

希望这会有所帮助:-)

答案 1 :(得分:8)

这是组件> 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中导入component-module,并在app.module.ts的入口组件中声明component-name。

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

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

答案 2 :(得分:4)

基本上,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';

const PAGES_COMPONENTS = [
  MyComponentComponent,
  MyComponentComponent2,
  MyComponentComponent3,
  MyComponentComponent4
];

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule.forRoot(),
  ],
  declarations: [
    PAGES_COMPONENTS
  ],
  exports: [
    PAGES_COMPONENTS
  ],
  entryComponents: [],
})
export class ComponentsModule {}

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

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

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  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-component>
<app-my-component3></app-my-component>
<app-my-component4></app-my-component>

希望这会有所帮助。

答案 3 :(得分:3)

完成上述所有操作后...

仅在home.page.html中有效 在我的组件名称之前添加 app

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

答案 4 :(得分:1)

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

步骤1:创建组件ionic g component components/FeaturedProduct

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

第3步:将FeaturedProductComponent导入feature-product.module.ts文件

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

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

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

现在该标签可以使用该组件 <app-featured-product></app-featured-product>

答案 5 :(得分:0)

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

我只是不明白为什么命令“离子生成组件组件/ myComponent --export”不再创建该过时的模块。我经历了同样的problem here

答案 6 :(得分:0)

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

离子性g组分组分/组分名称

将组件添加到html

答案 7 :(得分:-1)

在您的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>