问题
I'm trying to add a new menu item to the default generated JHipster app. I'm using Angular 4 by the way.
The problem I'm facing is that I'm always getting the error below.
Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'user-profile'
The following are the codes I added.
Firstly, I added a folder named user-profile under src/main/webapp/app.
In it, I added index.ts, user-profile.component.html, user-profile.component.ts and user-profile.route.ts
The contents of index.ts
export * from './user-profile.component';
export * from './user-profile.route';
The contents of user-profile.component.ts
import { Component, OnInit } from '@angular/core';
import { Principal } from '../shared';
@Component({
selector: 'jhi-user-profile',
templateUrl: './user-profile.component.html'
})
export class UserProfileComponent implements OnInit {
account: Account;
constructor(private principal: Principal) {}
ngOnInit() {
this.principal.identity().then((account) => {
this.account = account;
});
}
}
The contents of user-profile.route.ts
import { Route } from '@angular/router';
import { UserRouteAccessService } from '../shared';
import { UserProfileComponent } from './';
export const userProfileRoute: Route = {
path: 'user-profile',
component: UserProfileComponent,
data: {
authorities: [],
pageTitle: 'user-profile.title'
}
};
The contents of user-profile.component.html
<div>Hello World!</div>
I also modified navbar.html to include a new menu item with the following routerlink
routerLink="user-profile"
Any help to this problem will be greatly appreciated.
Thanks.
回答1:
You may need to load your router somewhere.
Try perform the following:
- Add a user-profile.module.ts in to the same directory as your user profile component:
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { RouterModule } from '@angular/router';
import { UserProfileComponent, userProfileRoute } from './';
@NgModule({
imports: [
RouterModule.forRoot([ userProfileRoute ], { useHash: true })
],
declarations: [
UserProfileComponent,
],
entryComponents: [
],
providers: [
]
})
export class UserProfileModule {}
- In the app.module.ts, add the following:
import { UserProfileModule } from './user-profile/user-profile.module';
and
imports: [
BrowserModule,
LayoutRoutingModule,
...
**UserProfileModule**
],
Hope it works.
来源:https://stackoverflow.com/questions/46587247/adding-new-route-to-jhipster-using-angular-4