简体   繁体   中英

Angular 5 - How to inject Service outside a component/service

I'm trying to use a service outside of a component.
My final goal would be to use my service on a function wrapped into a data object which I'd pass to my router to be used by my Breadcrumb latter on.

Here is an example of what I'd like to have :

{
    path: 'user-management/:login',
    component: UserMgmtDetailComponent,
    data: {
        pageTitle: 'userManagement.home.title',
        getChildBreadcrumb: (login) => {
            return 'User : ' + login;
        }
    }
}

And so I want my function inside getChildBreadcrumb to use a service, so it could retrieve any kind of value (for example the first name of the 'login' User).

I have been looking at Injector, but I am not sure it will do the job. Here is one I have been trying to implement, but I have a

"No provider for HttpClient !" error :

const injector = Injector.create({
  providers: [{
    provide: UserService,
    deps: [HttpClient]
  }]
});
let userService = injector.get(UserService);

If you have any clue why my solution is not working, or have any other solution, it would be great !

Thank you.

That generally happens when you don't have added HttpClientModule to your imports array of your NgModule . Try doing that first if you've not done that already.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM