Jhipster 4 How to put entity component (list) on home screen

两盒软妹~` 提交于 2019-12-05 07:07:20

What you're trying to accomplish with this code is rendering the component <jhi-customer> as a nested component of the home component (to list the top 10 customers you might require additional logic, depending on what "top" would mean).

You can do so by using the modules files and changing their visibility through exports/imports statements. The modules appeared in Angular RC6 and require you to define the accessibility rules between modules.

Suppose you have defined and generated the following JDL model:

entity Customer {
    name String required,
}

entity Task {
    name String required
}

relationship OneToMany {
    Customer{tasks} to Task{customer(name)}
}

Then you'll end up with:

  • customer.module.ts
  • entity.module.ts

In customer.module.ts, export CustomerComponent, as such:

@NgModule({
    imports: [
        JhipsterSharedModule,
        RouterModule.forRoot(ENTITY_STATES, { useHash: true })
    ],
    exports: [
        CustomerComponent
    ],
    declarations: [...],
    entryComponents: [...],
    providers: [...],
    schemas: [...]
})
export class JhipsterCustomerModule {}

In entity.module.ts, export the resulting module JhipsterCustomerModule:

@NgModule({
    imports: [
        JhipsterCustomerModule,
        JhipsterTaskModule
    ],
    exports: [
        JhipsterCustomerModule
    ],
    declarations: [],
    entryComponents: [],
    providers: [],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class JhipsterEntityModule {
}

as explained here

The last step is to import JhipsterEntityModule in home.module.ts:

@NgModule({
    imports: [
        JhipsterSharedModule,
        JhipsterEntityModule,
        RouterModule.forRoot([HOME_ROUTE], {useHash: true}),
    ],
    declarations: [
        HomeComponent
    ],
    entryComponents: [],
    bootstrap: [],
    providers: [],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class JhipsterHomeModule {
}

Then the component will be rendered appropriately. Here, I exported CustomerComponent and re-exported the full module but you might find a more precise scoping of imports/exports suitable for your needs.

If you're looking for the reference, have a look at NgModule section, which will take you to a step-by-step tutorial for dealing with the new Angular modules.

Nico

You have to register CustomerComponent in app/shared/shared.common.module.ts in declarations and exports sections. Placing it in other file did not work for me.

Here is a working description.

i.e

import { CustomComponent } from 'path/to/custom.component';

@NgModule({
    declarations: [
        ...
        QuillComponent,
    ],
    exports: [
        ...
        CustomComponent,
    ],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]

})
export class YourJhiSharedModule {}

NOTE: YourJhiSharedModule generally follow you app naming, but is found at app/shared/shared.module.ts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!