prevox
prevox

Reputation: 586

Initialize a service without a constructor

I'm trying to add i18n (integrated with ngx-translate) to my side menu, which is a constant object with each item inside

I've tried importing the i18n service and then declaring it as a variable to use it inside the object:

import { Menu } from './menu.model';
import { TranslateService } from '@ngx-translate/core';

let translate: TranslateService;

export const verticalMenuItems = [

    new Menu(1, translate.instant('MENU.DASHBOARD'), '/', null, 'dashboard', null, false, 0),
]

It won't get recognized as a service throwing the error

Cannot read property 'instant' of undefined

What am I doing wrong here?

Upvotes: 0

Views: 1077

Answers (1)

Ciprian Nazarie
Ciprian Nazarie

Reputation: 56

You are just declaring a variable of type TranslateService, which is uninitialized. You don't actually have an instance of the service in translate. I would suggest injecting the service into the constructor of the component where you are declaring your constant. I would do something like this.

export class ExampleClass {

    verticalMenuItems: any;
    constructor(private translate: TranslateService) {
        this.verticalMenuItems =
            [new Menu (1, translate.instant('MENU.DASHBOARD'), '/', null, 'dashboard', null, false, 0);
    }
}

Upvotes: 1

Related Questions