0
var regionUtils = {
  getNameByCode: function(code){
        var name = "ERROR";
        console.log("in getNameByCode :" + this.regions);// here this.regions is undefined.
        for(var i=0;i<this.regions.length;i++){
            if(this.regions[i]['code'] === code){
                name = this.regions[i]['name'];
            }
        }
        return name;

  },
  regions:[
        {
            "name": "Afghanistan",
            "alpha2": "AF",
            "alpha3": "AFG",
            "numeric": "004"
        },
        {
            "name": "Åland Islands",
            "alpha2": "AX",
            "alpha3": "ALA",
            "numeric": "248",
            "altName": "Aland Islands"
        },
  ],
};

export default regionUtils;

And in other scripts when I imported the above util and trying to use the method getNameByCode and find this.regions is undefined.

I am using this function in a VUE script like this:

import RegionUtils from '@/utils/regionUtils'
export default {
    methods:{
      getCodeByName: RegionUtils.getCodeByName,
    }
}

and using that in my template

<td><div v-for="item in scene.regions" :key="item.name">{{getNameByCode(item)}}</div></td>
armnotstrong
  • 8,605
  • 16
  • 65
  • 130

0 Answers0