Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Sign InSign Up

Softans

Softans Logo Softans Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 1648
Answered
Max
Max
Asked: April 26, 20222022-04-26T11:15:24+00:00 2022-04-26T11:15:24+00:00

@Injectable giving undefined in angular 13

Hello guys I’ve recently updated angular to the version 13 and I’m experiencing some issues regarding DI. When I call the ReflectiveInjector.resolveAndCreate() function it gives me undefined error. What may be the possible reason behind this one guys, do you maybe know ? I’m having problems with ScheduleProvider which is decorated by @Injectable() and injected inside my pro-container.js file.

Here are the connected parts:

schedule-provider.js

import { Injectable } from 'injection-js';

import { MzpackgHttpService, MzpackgLightResource, MzpackgListFilter } from 'mzpackg-api-schedules';

@Injectable()
export class ScheduleProvider {
public schedule= {
/*
  some data
*/
};

public constructor(private readonly mzPackgHttpService: MzpackgHttpService) {}
}

pro-container.js

import { InjectionToken, Provider, ReflectiveInjector } from 'injection-js';

import {
 RolesPartiesHttpService,
 HttpService,
} from 'mzpackg-api-roles';

import {PaymentsHttpService } from 'mzpackg-api-payments';
import { LawHttpService } from 'mzpackg-api-law';
import {
TasksHttpService,
} from 'mzpackg-api-tasks';
import { BrokersHttpService } from 'mzpackg-api-brokers';
import {
 HttpService as ExtendedPaymentHttpService
} from 'mzpackg-api-extpayments';

import { ScheduleProvider } from './schedule-provider';

export const BROKERS_HTTP_SERVICE = new InjectionToken<HttpService>('broker http service');
export const PAYMENTS_HTTP_SERVICE = new InjectionToken<ExtendedPaymentHttpService>('payments http service');

export class ProContainer{
  private readonly providers: Provider[];

  public constructor() {
    this.providers = [
      {
        provide: BrokersHttpService,
        useFactory: (httpService: HttpService) => new BrokersHttpService(httpService),
        deps: [BROKERS_HTTP_SERVICE]
      },      
      {
        provide: PaymentsHttpService,
        useFactory: (httpService: HttpService) => new PaymentsHttpService(httpService),
        deps: [PAYMENTS_HTTP_SERVICE]
      },
       {
        provide: ScheduleProvider,
        useFactory: (tasksHttpService: TasksHttpService) => new 
ScheduleProvider(tasksHttpService),
        deps: [TasksHttpService, RolesPartiesHttpService, LawHttpService]
       },
    ];
  }

}

The problem is when I comment ScheduleProvider it works, so ScheduleProvider is giving me undefined error. My angular packages versions are :

@angular-devkit/architect       0.1302.6

@angular-devkit/build-angular   13.2.6

@angular-devkit/core            13.2.6

@angular-devkit/schematics      13.2.6

@angular/cdk                    13.2.6

@angular/cli                    13.2.6

@angular/compiler-cli           13.2.6

@angular/language-service       13.2.6

@angular/material               13.2.6

@schematics/angular             13.2.6

rxjs                            6.6.7

typescript                      4.5.5

Any help is highly appreciated.

angular
  • 0
  • 1 1 Answer
  • 44 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  1. Best Answer
    Max
    2022-04-26T11:16:00+00:00Added an answer on April 26, 2022 at 11:16 am

    The official docs days:

    Deprecated: from v5 – slow and brings in a lot of code, Use Injector.create instead.

    Usage (from official docs):

    const injector: Injector =
        Injector.create({providers: [{provide: 'validToken', useValue: 'Value'}]});
    expect(injector.get('validToken')).toEqual('Value');
    expect(() => injector.get('invalidToken')).toThrowError();
    expect(injector.get('invalidToken', 'notFound')).toEqual('notFound');
    

    Injector returns itself when given Injector as a token:

    const injector = Injector.create({providers: []});
    expect(injector.get(Injector)).toBe(injector);
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • Ghulam Nabi

    Why are the British confused about us calling bread rolls ...

    • 5 Answers
  • Alex

    application has failed to start because no appropriate graphics hardware ...

    • 4 Answers
  • Jerry

    Add file to native target programmatically via tuist/XcodeProj

    • 4 Answers
  • Ghulam Nabi
    Ghulam Nabi added an answer To resolve the NullPointerException, you need to identify the variable… March 15, 2023 at 8:25 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can replace the PnP code in your Azure Function… February 13, 2023 at 7:11 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can use the $match stage in the aggregate pipeline… February 10, 2023 at 6:20 am

Trending Tags

android c++ cypress flutter java javascript python selenium testng webdriver

Top Members

Robert

Robert

  • 3 Questions
  • 1k Points
Luci

Luci

  • 5 Questions
  • 1k Points
Kevin O Brien

Kevin O Brien

  • 2 Questions
  • 1k Points

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Softans

Softans is a social questions & Answers Engine which will help you establish your community and connect with other people.

About Us

  • Blog
  • Jobs
  • About Us
  • Meet The Team
  • Contact Us

Legal Stuff

Help

Follow

© 2021 Softans. All Rights Reserved
With Love by Softans.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.