繁体   English   中英

无法通过使用美国的 libphonenumber-js 键入国家/地区呼叫代码来识别国家/地区代码

[英]can't identify country code by typing country calling code using libphonenumber-js for US

我正在使用libphonenubmer-js以在国家呼叫代码步骤中键入电话号码时显示国家国旗:

  const asYouType = new AsYouType();
  asYouType.input(`+${callingCode}`);

  console.log({ asYouType.country })

问题是对于法国号码( +33 | 33前缀), FR会立即被识别。 但是对于美国( +1 | 1前缀),它不会。

反正周围有吗? w/o 输入整数?

原因是 +1 前缀被更多国家使用,而不仅仅是美国。 https://en.wikipedia.org/wiki/List_of_country_calling_codes

是的,确实这是一个鸽洞原理案例。

决定使用第一个可行的选项:

const countryCallingCode = e.target.value;
const asYouType = new AsYouType();
asYouType.input(`+${countryCallingCode}`);

let currentCountyCode = asYouType.country;

if (!currentCountyCode) {
    const matchingCountriesArray = asYouType.metadata.metadata.country_calling_codes[
        countryCallingCode
    ];

    if (matchingCountriesArray) {
        currentCountyCode = matchingCountriesArray[0];
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM