Ionic 4自定义组件 [英] Ionic 4 custom components
问题描述
运行命令ionic g component myComponent
后如何在ionic 4中加载组件?我想在主页上添加一个新生成的自定义组件.
How do you load a component in ionic 4 after running the command ionic g component myComponent
? I want to add a new generated custom component to my home page.
推荐答案
最后弄清楚了,这是一个有效的复制程序:
Finally figured this out, here's a repro that works:
ionic start myProject blank --type=angular
ionic g module components
ionic g component components/myComponent --export
这会将声明和导出添加到"myComponent"的组件模块中.
This adds both a declaration and export to the components module for "myComponent".
要使用该组件,只需将ComponentsModule
添加到页面模块中的imports
中,例如
To use the component just add ComponentsModule
to your imports
in your page module, e.g.
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
ComponentsModule,
RouterModule.forChild([
{
path: '',
component: HomePage
}
])
],
declarations: [HomePage]
})
export class HomePageModule { }
这样,app.module.ts
不会添加任何内容,应该是这样.
This way nothing is added to app.module.ts
which is how it should be.
还请注意,如果要在自己的自定义组件中使用ANY组件,则需要将IonicModule
添加到components.module.ts
Also note if you want to use ANY components in your own custom components, you need to add IonicModule
to the imports
in components.module.ts
希望这会有所帮助:-)
Hope this helps :-)
这篇关于Ionic 4自定义组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!