Ionic 4自定义组件 [英] Ionic 4 custom components

查看:142
本文介绍了Ionic 4自定义组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行命令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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆