本文介绍了如何在Angular 6中使用i18n-iso-countries的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angualr 6应用程序中,我试图使用i18n-iso-countries库(JavaScript)将国家/地区代码转换为国家/地区名称.我使用npm

In my Angualr 6 application I am trying to convert country codes to country names using i18n-iso-countries library (JavaScript).I installed it using npm

然后在我的组件中按如下所示导入它:

Then in my component I imported it like this:

我正在这样使用它:

  let valueName = '';
  tmpArr = tmpArr.map((e) => {
      const tmp = e.split('_')[1];
      console.log('tmp: ' + tmp);
      // console.log(i18IsoCountries);
      valueName = i18IsoCountries.getName(tmp, 'en');
      return tmp;
    }

但是valueName是未定义的.

However valueName is undefined.

我还在此处的Stackblitz中创建了类似的设置.您可以看到getName的输出未定义,但是getAlpha2Codes()的输出返回值.

I also created similar setup in Stackblitz here.You can see that output from getName is undefined but output from getAlpha2Codes() returns value.

推荐答案

我刚刚发现该语言需要注册.该文档有点混乱

I just found out that language needs to be registered. The documentation is a little confusing

但是您要做需要注册要使用的语言.

But you do need to register the languages that you want to use.

因此在Angular 6中,您需要:

So in Angular 6 you need to:

然后在您的组件中:

从"i18n-iso-countries"中导入*作为i18nIsoCountries;

然后在ngOnInit()

And then in ngOnInit()

i18nIsoCountries.registerLocale(require("i18n-iso-countries/langs/en.json"));

这篇关于如何在Angular 6中使用i18n-iso-countries的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 18:38