Tin*_*ali 4 typescript jhipster angular
我正在尝试在jhipster生成的Angular 5应用程序上使用component另一个应用程序。module
当module导入包含component我要使用的 的时,route发生module导入的 的 会被route导入的覆盖module。
phone.module.ts出口PhoneComponent。contact-info.module.ts进口phone.module.ts。route声明的contact-info.route.ts开始渲染 中声明的组件phone.module.ts。看来电话模块已被完全覆盖。phone.module.tsimport { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { RouterModule } from '@angular/router';
import { JhiLanguageService } from 'ng-jhipster';
import { JhiLanguageHelper } from 'app/core';
import { FrontendSharedModule } from 'app/shared';
import {
PhoneComponent,
PhoneDetailComponent,
PhoneUpdateComponent,
PhoneDeletePopupComponent,
PhoneDeleteDialogComponent,
phoneRoute,
phonePopupRoute
} from './';
const ENTITY_STATES = [...phoneRoute, ...phonePopupRoute];
@NgModule({
imports: [FrontendSharedModule, RouterModule.forChild(ENTITY_STATES)],
declarations: [PhoneComponent, PhoneDetailComponent, PhoneUpdateComponent, PhoneDeleteDialogComponent, PhoneDeletePopupComponent],
entryComponents: [PhoneComponent, PhoneUpdateComponent, PhoneDeleteDialogComponent, PhoneDeletePopupComponent],
providers: [{ provide: JhiLanguageService, useClass: JhiLanguageService }],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
exports: [PhoneComponent]
})
export class BurocracyPhoneModule {
constructor(private languageService: JhiLanguageService, private languageHelper: JhiLanguageHelper) {
this.languageHelper.language.subscribe((languageKey: string) => {
if (languageKey !== undefined) {
this.languageService.changeLanguage(languageKey);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
contact-info.module.tsimport { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { RouterModule } from '@angular/router';
import { JhiLanguageService } from 'ng-jhipster';
import { JhiLanguageHelper } from 'app/core';
import { FrontendSharedModule } from 'app/shared';
import {
ContactInfoComponent,
ContactInfoDetailComponent,
ContactInfoUpdateComponent,
ContactInfoDeletePopupComponent,
ContactInfoDeleteDialogComponent,
contactInfoRoute,
contactInfoPopupRoute
} from './';
import { BurocracyPhoneModule } from 'app/entities/burocracy/phone/phone.module';
const ENTITY_STATES = [...contactInfoRoute, ...contactInfoPopupRoute];
@NgModule({
imports: [FrontendSharedModule, BurocracyPhoneModule, RouterModule.forChild(ENTITY_STATES)],
declarations: [
ContactInfoComponent,
ContactInfoDetailComponent,
ContactInfoUpdateComponent,
ContactInfoDeleteDialogComponent,
ContactInfoDeletePopupComponent
],
entryComponents: [ContactInfoComponent, ContactInfoUpdateComponent, ContactInfoDeleteDialogComponent, ContactInfoDeletePopupComponent],
providers: [{ provide: JhiLanguageService, useClass: JhiLanguageService }],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class BurocracyContactInfoModule {
constructor(private languageService: JhiLanguageService, private languageHelper: JhiLanguageHelper) {
this.languageHelper.language.subscribe((languageKey: string) => {
if (languageKey !== undefined) {
this.languageService.changeLanguage(languageKey);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
http://localhost:8080/#/contact-info 导入前BurocracyPhoneModule

http://localhost:8080/#/contact-info 导入后BurocracyPhoneModule

以下所有文件均由 生成jhipster。
phone.route.tsimport { Injectable } from '@angular/core';
import { HttpResponse } from '@angular/common/http';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot, Routes } from '@angular/router';
import { JhiPaginationUtil, JhiResolvePagingParams } from 'ng-jhipster';
import { UserRouteAccessService } from 'app/core';
import { Observable, of } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { Phone } from 'app/shared/model/burocracy/phone.model';
import { PhoneService } from './phone.service';
import { PhoneComponent } from './phone.component';
import { PhoneDetailComponent } from './phone-detail.component';
import { PhoneUpdateComponent } from './phone-update.component';
import { PhoneDeletePopupComponent } from './phone-delete-dialog.component';
import { IPhone } from 'app/shared/model/burocracy/phone.model';
@Injectable({ providedIn: 'root' })
export class PhoneResolve implements Resolve<IPhone> {
constructor(private service: PhoneService) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<IPhone> {
const id = route.params['id'] ? route.params['id'] : null;
if (id) {
return this.service.find(id).pipe(
filter((response: HttpResponse<Phone>) => response.ok),
map((phone: HttpResponse<Phone>) => phone.body)
);
}
return of(new Phone());
}
}
export const phoneRoute: Routes = [
{
path: '',
component: PhoneComponent,
resolve: {
pagingParams: JhiResolvePagingParams
},
data: {
authorities: ['ROLE_USER'],
defaultSort: 'id,asc',
pageTitle: 'frontendApp.burocracyPhone.home.title'
},
canActivate: [UserRouteAccessService]
},
{
path: ':id/view',
component: PhoneDetailComponent,
resolve: {
phone: PhoneResolve
},
data: {
authorities: ['ROLE_USER'],
pageTitle: 'frontendApp.burocracyPhone.home.title'
},
canActivate: [UserRouteAccessService]
},
{
path: 'new',
component: PhoneUpdateComponent,
resolve: {
phone: PhoneResolve
},
data: {
authorities: ['ROLE_USER'],
pageTitle: 'frontendApp.burocracyPhone.home.title'
},
canActivate: [UserRouteAccessService]
},
{
path: ':id/edit',
component: PhoneUpdateComponent,
resolve: {
phone: PhoneResolve
},
data: {
authorities: ['ROLE_USER'],
pageTitle: 'frontendApp.burocracyPhone.home.title'
},
canActivate: [UserRouteAccessService]
}
];
export const phonePopupRoute: Routes = [
{
path: ':id/delete',
component: PhoneDeletePopupComponent,
resolve: {
phone: PhoneResolve
},
data: {
authorities: ['ROLE_USER'],
pageTitle: 'frontendApp.burocracyPhone.home.title'
},
canActivate: [UserRouteAccessService],
outlet: 'popup'
}
];
Run Code Online (Sandbox Code Playgroud)
contact-info.route.tsimport { Injectable } from '@angular/core';
import { HttpResponse } from '@angular/common/http';
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot, Routes } from '@angular/router';
import { JhiPaginationUtil, JhiResolvePagingParams } from 'ng-jhipster';
import { UserRouteAccessService } from 'app/core';
import { Observable, of } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { ContactInfo } from 'app/shared/model/burocracy/contact-info.model';
import { ContactInfoService } from './contact-info.service';
import { ContactInfoComponent } from './contact-info.component';
import { ContactInfoDetailComponent } from './contact-info-detail.component';
import { ContactInfoUpdateComponent } from './contact-info-update.component';
import { ContactInfoDeletePopupComponent } from './contact-info-delete-dialog.component';
import { IContactInfo } from 'app/shared/model/burocracy/contact-info.model';
@Injectable({ providedIn: 'root' })
export class ContactInfoResolve implements Resolve<IContactInfo> {
constructor(private service: ContactInfoService) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<IContactInfo> {
const id = route.params['id'] ? route.params['id'] : null;
if (id) {
return this.service.find(id).pipe(
filter((response: HttpResponse<ContactInfo>) => response.ok),
map((contactInfo: HttpResponse<ContactInfo>) => contactInfo.body)
);
}
return of(new ContactInfo());
}
}
export const contactInfoRoute: Routes = [
{
path: '',
component: ContactInfoComponent,
resolve: {
pagingParams: JhiResolvePagingParams
},
data: {
authorities: ['ROLE_USER'],
defaultSort: 'id,asc',
pageTitle: 'frontendApp.burocracyContactInfo.home.title'
},
canActivate: [UserRouteAccessService]
},
{
path: ':id/view',
component: ContactInfoDetailComponent,
resolve: {
contactInfo: ContactInfoResolve
},
data: {
authorities: ['ROLE_USER'],
pageTitle: 'frontendApp.burocracyContactInfo.home.title'
},
canActivate: [UserRouteAccessService]
},
{
path: 'new',
component: ContactInfoUpdateComponent,
resolve: {
contactInfo: ContactInfoResolve
},
data: {
authorities: ['ROLE_USER'],
pageTitle: 'frontendApp.burocracyContactInfo.home.title'
},
canActivate: [UserRouteAccessService]
},
{
path: ':id/edit',
component: ContactInfoUpdateComponent,
resolve: {
contactInfo: ContactInfoResolve
},
data: {
authorities: ['ROLE_USER'],
pageTitle: 'frontendApp.burocracyContactInfo.home.title'
},
canActivate: [UserRouteAccessService]
}
];
export const contactInfoPopupRoute: Routes = [
{
path: ':id/delete',
component: ContactInfoDeletePopupComponent,
resolve: {
contactInfo: ContactInfoResolve
},
data: {
authorities: ['ROLE_USER'],
pageTitle: 'frontendApp.burocracyContactInfo.home.title'
},
canActivate: [UserRouteAccessService],
outlet: 'popup'
}
];
Run Code Online (Sandbox Code Playgroud)
entity.module.tsimport { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { RouterModule } from '@angular/router';
@NgModule({
imports: [
RouterModule.forChild([
{
path: 'contact-info',
loadChildren: './burocracy/contact-info/contact-info.module#BurocracyContactInfoModule'
},
{
path: 'phone',
loadChildren: './burocracy/phone/phone.module#BurocracyPhoneModule'
}
/* jhipster-needle-add-entity-route - JHipster will add entity modules routes here */
])
],
declarations: [],
entryComponents: [],
providers: [],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class FrontendEntityModule {}
Run Code Online (Sandbox Code Playgroud)
我设法解决了这个问题。实际上,它看起来更像是一种解决方法,而不是一个适当的解决方案,但它可以完成工作。
事实证明,Angular 对于如何处理非常有讲究routes,并且(显然)导出 a 的正确方法route是导出为 aclass而不是导出为constant。此外,由于Angular 错误,必须在模块之前route导入。
@Injectable({ providedIn: 'root' })
export class ContactInfoResolve implements Resolve<IContactInfo> {
constructor(private service: ContactInfoService) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<IContactInfo> {
const id = route.params['id'] ? route.params['id'] : null;
if (id) {
return this.service.find(id).pipe(
filter((response: HttpResponse<ContactInfo>) => response.ok),
map((contactInfo: HttpResponse<ContactInfo>) => contactInfo.body)
);
}
return of(new ContactInfo());
}
}
// constants are not imported anymore
const contactInfoRoute: Routes = [
// ...
]
const contactInfoPopupRoute: Routes = [
// ...
]
// This is the juice part
const ROUTE = [...contactInfoRoute, ...contactInfoPopupRoute];
@NgModule({
imports: [RouterModule.forChild(ROUTE)],
exports: [RouterModule]
})
export class InfoRoute { }
Run Code Online (Sandbox Code Playgroud)
import {
ContactInfoComponent,
ContactInfoDetailComponent,
ContactInfoUpdateComponent,
ContactInfoDeletePopupComponent,
ContactInfoDeleteDialogComponent,
InfoRoute
} from './';
@NgModule({
imports: [
// importing routes as a class
InfoRoute,
BurocracyPhoneModule,
FrontendSharedModule
],
declarations: [
ContactInfoComponent,
ContactInfoDetailComponent,
ContactInfoUpdateComponent,
ContactInfoDeleteDialogComponent,
ContactInfoDeletePopupComponent
],
entryComponents: [ContactInfoComponent, ContactInfoUpdateComponent, ContactInfoDeleteDialogComponent, ContactInfoDeletePopupComponent],
providers: [{ provide: JhiLanguageService, useClass: JhiLanguageService }],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class BurocracyContactInfoModule {
constructor(private languageService: JhiLanguageService, private languageHelper: JhiLanguageHelper) {
this.languageHelper.language.subscribe((languageKey: string) => {
if (languageKey !== undefined) {
this.languageService.changeLanguage(languageKey);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
我对这个解决方案不太满意,因为我认为它不是很优雅,但它对我有用。如果有人有其他方法来解决这个问题,请与我们分享。
| 归档时间: |
|
| 查看次数: |
1483 次 |
| 最近记录: |