使用Angular 4为Jhipster添加新路由

Qcu*_*ber 2 jhipster angular angular-router

我正在尝试将新菜单项添加到默认生成的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';
Run Code Online (Sandbox Code Playgroud)

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;
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

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'
    }
}; 
Run Code Online (Sandbox Code Playgroud)

user-profile.component.html的内容

<div>Hello World!</div>
Run Code Online (Sandbox Code Playgroud)

我还修改了navbar.html以包含一个带有以下routerlink的新菜单项

routerLink="user-profile"
Run Code Online (Sandbox Code Playgroud)

对此问题的任何帮助将不胜感激.

谢谢.

小智 7

您可能需要在某处加载路由器.

尝试执行以下操作:

  1. 将user-profile.module.ts添加到与用户配置文件组件相同的目录中:


    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 {}

Run Code Online (Sandbox Code Playgroud)
  1. 在app.module.ts中,添加以下内容:


    import { UserProfileModule } from './user-profile/user-profile.module';

Run Code Online (Sandbox Code Playgroud)



    imports: [
        BrowserModule,
        LayoutRoutingModule,
        ...
        **UserProfileModule**
    ],

Run Code Online (Sandbox Code Playgroud)

希望它有效.