Angular 离子4定制组件
运行命令后,如何在ionic 4中加载组件?我想将新生成的自定义组件添加到我的主页。在您的src/components文件夹中 myComponent.html:Angular 离子4定制组件,angular,typescript,ionic-framework,ionic4,Angular,Typescript,Ionic Framework,Ionic4,运行命令后,如何在ionic 4中加载组件?我想将新生成的自定义组件添加到我的主页。在您的src/components文件夹中 myComponent.html: //Here your component HTML Code. For example: <ion-list radio-group (ionSelect)="change($event, item.type);" ... </ion-list> import {myComponentComponent}
//Here your component HTML Code. For example:
<ion-list radio-group (ionSelect)="change($event, item.type);"
...
</ion-list>
import {myComponentComponent} from "./myComponent/myComponent";
@NGModule({
declatations: [myComponentComponent],
imports:[],
exports: [myComponentComponent]
})
export class ComponentsModule{}
import { MyComponentComponent } from "../components/myComponent/myComponent";
@NGModule({
declarations: [
yourPages....,
MyComponentComponent
]
)}
在src/app文件夹中
应用程序模块.ts:
//Here your component HTML Code. For example:
<ion-list radio-group (ionSelect)="change($event, item.type);"
...
</ion-list>
import {myComponentComponent} from "./myComponent/myComponent";
@NGModule({
declatations: [myComponentComponent],
imports:[],
exports: [myComponentComponent]
})
export class ComponentsModule{}
import { MyComponentComponent } from "../components/myComponent/myComponent";
@NGModule({
declarations: [
yourPages....,
MyComponentComponent
]
)}
要使用它:
例如,在HomePage.html中:
<myComponent> </myComponent>
终于明白了这一点,下面是一个可以正常工作的复制程序:
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添加任何内容,这就是它应该添加的内容
另请注意,如果您想在自己的自定义组件中使用任何组件,则需要将IonicModule
添加到components.module.ts中的imports
希望这有帮助:-)这是您在组件>脚卡>脚卡中的选择器。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 {}
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 {}
这是您的app.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 {}
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 {}
您必须在导入中导入组件模块,并在app.module.ts的条目组件中声明组件名称
在components.module.ts中,您必须声明并导出组件,但不要忘记导入IonicModule
我也面临同样的问题,但没有人告诉我在Components.module.ts和app.module.ts中导入IonicModule,只添加到entryComponent和导入componentModule 关键是要包括一个新模块,专门用于定制组件
我只是不明白为什么命令“ionic generate component/myComponent--export”不再创建这个过时的模块。我也经历了同样的过程。在完成上述所有操作之后
仅适用于home.page.html
在我的组件名称之前添加app,如:
<app-my-component></app-my-component>
基本上,爱奥尼亚g组件myComponent
将更新app.module.ts并在app文件夹中创建组件
但是如果您想要一种更优雅的方式来添加组件。以下是步骤:
ionic g module 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
将组件模块导入测试组件/页面或(类似地导入当前主页):
最后,只需使用组件选择器在测试页面中编写组件。e、 g
<app-my-component></app-my-component>
<app-my-component2></app-my-component2>
<app-my-component3></app-my-component3>
<app-my-component4></app-my-component4>
希望这会有所帮助。只需输入下面的命令即可生成自定义组件
离子g组分/组分名称
将组件添加到html
在离子6.11.0上测试此溶液。工作完美。
创建名为“FeaturedProduct”的可重用组件的流程-
步骤1:创建组件g组件组件/功能产品
步骤2:为此组件创建一个模块。
ionic g模块组件/功能产品
步骤3:将FeaturedProductComponent导入featured-product.module.ts文件
@NgModule({
declarations: [FeaturedProductComponent],
exports: [FeaturedProductComponent],
imports: [
CommonModule
]
})
步骤4:在所需页面的module.ts文件上导入FeaturedProductModule
@NgModule({
imports: [
...
...
FeaturedProductModule
],
declarations: [Tab2Page]
})
现在该组件可由该标记使用
“mycomponent”不是已知元素:1。如果“mycomponent”是一个角度组件,则验证它是否是此模块的一部分。2.若要允许任何元素,请在该组件的“@NgModule.schemas”中添加“NO\u ERRORS\u SCHEMA”。我忘记了components.module.tsi,我想您必须将app.module.ts声明编辑为yes,如我的回答所示。在这里我描述了在app.module.tsno中要做什么请注意,在ionic 4中,它被证明很难尝试做itI,实际上它有完全相同的问题,它也很容易复制。只需启动一个新项目:ionic start project blank--type=angular。然后生成一个组件:ionic g component timer,最后添加到home.page.html,它会给出“app timer不是已知元素”错误。我已经通读了很多v4文档,但毫无用处。我意识到这并不能解决问题,但我想我应该提供一份报告。编辑:换行符丢失,对此表示抱歉。不知何故,它适用于一个空白的新项目,但在我现有的项目中,它加载了一个空白页面,没有错误报告。非常感谢。如果我想在任何页面中使用单个组件,我发现这非常有用。。?那么我还需要将整个componentModule导入到我的页面模块吗?出于某种原因,--export标志对我不起作用,但将MyComponentComponent添加到componentModule声明和导出数组手动起作用。glmdev的注释起作用,但在html中该组件显示“未找到元素”。在我将myComponent添加到home.module.ts中的EntryComponents之后,它对我很有效。非常好的解释。非常感谢。