Q:

ERROR TypeError: Cannot read property 'flag' of undefined

I have a problem with loading images in Angular 4, I found how to solve it by creating a variable in the ts file and using [src]="variablename". However, I have now another problem, I want to set the variable using a concatenation but with the code that I have I got:
"ERROR TypeError: Cannot read property 'flag' of undefined"

The piece of code I want to use is: 
flag:any = require("../../../assets/images/flags/"+this.selectedLanguage.flag+".png");

How can I successfully use this so it can be declared?, for example, as:
flag:any = require("../../../assets/images/flags/ge.png");

The whole code I have is this:
import { Component } from '@angular/core';
import { NavigationEnd, NavigationStart, Router } from '@angular/router';
import { FuseConfigService } from '../../core/services/config.service';
import { TranslateService } from '@ngx-translate/core';
import { lang } from 'moment';

@Component({
    selector   : 'fuse-toolbar',
    templateUrl: './toolbar.component.html',
    styleUrls  : ['./toolbar.component.scss']
})

export class FuseToolbarComponent
{
    userStatusOptions: any[];
    languages: any;
    selectedLanguage: any;
    showLoadingBar: boolean;
    horizontalNav: boolean;

    constructor(
        private router: Router,
        private fuseConfig: FuseConfigService,
        private translate: TranslateService
    )

    {
        this.userStatusOptions = [
            {
                'title': 'Online',
                'icon' : 'icon-checkbox-marked-circle',
                'color': '#4CAF50'
            },
            {
                'title': 'Away',
                'icon' : 'icon-clock',
                'color': '#FFC107'
            },
            {
                'title': 'Do not Disturb',
                'icon' : 'icon-minus-circle',
                'color': '#F44336'
            },
            {
                'title': 'Invisible',
                'icon' : 'icon-checkbox-blank-circle-outline',
                'color': '#BDBDBD'
            },
            {
                'title': 'Offline',
                'icon' : 'icon-checkbox-blank-circle-outline',
                'color': '#616161'
            }
        ];

        this.languages = [
            {
                'id': 'uk',
                'title': 'English',
                'flag': 'uk'
            },
            {
                'id': 'ge',
                'title': 'German',
                'flag': 'ge'

            },
            {
                'id': 'es',
                'title': 'Spanish',
                'flag': 'es'
            },
            {
                'id': 'fr',
                'title': 'French',
                'flag': 'fr'
            }
        ];

        this.selectedLanguage = this.languages[0];
        console.log(this.selectedLanguage.flag);

        router.events.subscribe(
            (event) => {
                if ( event instanceof NavigationStart )
                {
                    this.showLoadingBar = true;
                }
                if ( event instanceof NavigationEnd )
                {
                    this.showLoadingBar = false;
                }
            });

        this.fuseConfig.onSettingsChanged.subscribe((settings) => {
            this.horizontalNav = settings.layout.navigation === 'top';
        });

    }

    search(value)
    {
        // Do your search here...
        console.log(value);
    }

    setLanguage(lang)
    {
        // Set the selected language for toolbar
        this.selectedLanguage = lang;

        // Use the selected language for translations
        this.translate.use(lang.id);

    }

    logo:any = require("../../../assets/images/logos/logo.png");
    avatarGuest:any = require("../../../assets/images/avatars/profile.jpg");
    flag:any = require("../../../assets/images/flags/"+this.selectedLanguage.flag+".png");

}

A:

It looks like the properties logo, avatarGuest and flag are initialised before the body of the constructor is called, so by the time you are getting the path for the flag this.selectedLanguage is indeed undefined. You can simply move this line into the body of the constructor after you initialise this.selectedLanguage:
// ... your code
this.selectedLanguage = this.languages[0];     
console.log(this.selectedLanguage.flag);

flag:any = require("../../../assets/images/flags/"+this.selectedLanguage.flag+".png");

