我正在尝试将新菜单项添加到默认生成的JHipster应用中。我顺便使用Angular 4。
我面临的问题是,我总是得到以下错误。
错误:未捕获(在承诺中):错误:无法匹配任何路由。网址细分:'用户资料'
以下是我添加的代码。
首先,我在src / main / webapp / app。
下添加了一个名为user-profile的文件夹在其中,我添加了index.ts,user-profile.component.html,user-profile.component.ts和user-profile.route.ts
index.ts的内容
export * from './user-profile.component';
export * from './user-profile.route';
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;
});
}
}
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'
}
};
user-profile.component.html
的内容<div>Hello World!</div>
我还修改了navbar.html以包含一个带有以下routerlink的新菜单项
routerLink="user-profile"
非常感谢对此问题的任何帮助。
感谢。
答案 0 :(得分:7)
您可能需要在某处加载路由器。
尝试执行以下操作:
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 {}
import { UserProfileModule } from './user-profile/user-profile.module';
和
imports: [
BrowserModule,
LayoutRoutingModule,
...
**UserProfileModule**
],
希望它有效。