Adding new route to Jhipster using Angular 4

为君一笑 提交于 2019-12-08 03:08:08

问题


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:

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

  1. 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

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