angularangular4-formsangular4-httpclient

How to pass locale in get service call using angular4


Need to find the locale and pass the same to service(get) call :

Property 'indexOf' does not exist on type '() => string[]'

Heading

in angular4.

//Call Languages 
getshortLanguages() {      
        return new Array('en', 'es', 'fr', 'it', 'pt', 'de', 'ru', 'tr', 'ko', 'ja', 'zh-cn', 'zh-hk');
    }

    getLocale() {
        const locale = this.dbservice.getSessionContext().locale.toLowerCase();
        var Languages = this.getshortLanguages;
        if (!locale) return 'en';     
        var languageCode = locale.split('-')[0].toLowerCase();    
        if (this.getshortLanguages.indexof(languageCode)>-1 ) {
            return languageCode;
        }
        else return 'en';
    }

Solution

  • You are most probably trying to use a getter right?

    In order to do this, you need to separate the get from the name like this:

    //Call Languages 
    get shortLanguages() {      
        return new Array('en', 'es', 'fr', 'it', 'pt', 'de', 'ru', 'tr', 'ko', 'ja', 'zh-cn', 'zh-hk');
    }
    
    getLocale() {
        const locale = this.dbservice.getSessionContext().locale.toLowerCase();
        var Languages = this.getshortLanguages;
        if (!locale) return 'en';     
        var languageCode = locale.split('-')[0].toLowerCase();    
        if (this.shortLanguages.indexof(languageCode)>-1 ) {
            return languageCode;
        }
        else return 'en';
    }
    

    Otherwise, you should got the way Igor Mentioned. In your case the getShortLanguages without the parentheses () is nothing more than the definition of your function and not an array at all.