#how inject service ?

9 messages · Page 1 of 1 (latest)

pale fern
#

angular 10

export const comService = Inject(ComProfilService);

export const ConfigEmploye: AcTableColumn[] = [
  {
    key: 'see',
    buttons: [
      {
        label: '<i class="fa fa-eye"></i>',
        action: (element, column) => {

          comService.emit({code:'', description: '', label:''} as IProfil);
        }
      }
    ],

  }
];
  emit(profil: IProfil) {
    this.profilSelectedEvent$.next(profil);
  }

no error at compile time but at runtime

core.js:4197 ERROR TypeError: comService.emit is not a function
    at Object.action (config-employe.ts:28:22)

Is this the right way to do it? Or is there another technique?

vale roost
#

unrelated, but why are you using such an old version of Angular. v10 is 2.5 years old.

pale fern
#

not me, it's my company 😦

#

a solution for angular 10?

vale roost
#

Yes. use the link I gave to the Angular 10 documentation to understand what DI is and how it's used.

#

Inject is a decorator. Not a function you can call to get some service. Just use DI as documented.

pale fern
#

I read but I don't see anything interesting
because I am in a const

vale roost
#

Well, you can't initialize global consts with DI. DI is precisely designed to avoid using global variables. Use services, and inject dependencies they have using DI. Inject thoses services in your components, pipes, directives, etc. Don't use global variables.