我有一个用Vue制作的自定义表单,有一个移动号码的字段,在那里我使用Vue Int电话输入包为国家代码下拉与旗帜。
我从国家/地区代码下拉列表中获得所选输入的国家/地区拨号代码值。我想从拨号代码值中获取国家代码。例如,如果印度是选定的国家/地区,并且我的拨号代码值为+91,我应该如何从该特定的拨号代码值中推断出国家代码,即IN?
**我可以分别获取这两个值,但我不能从拨号代码推导出国家代码。
任何帮助都将不胜感激!
发布于 2021-01-27 17:19:46
由于vue-tel-input在内部使用libphonenumber-js,因此您也可以使用它:
<template>
<vue-tel-input ref="tel" v-model="phone" />
</template>import parsePhoneNumberFromString from 'libphonenumber-js';
export default
{
data()
{
return {
phone: '',
};
},
methods:
{
getCountryCode()
{
// vue-tel-input does not update the model if you have a default country,
// so we have to access its internal representation
// To avoid accessing internal data of vue-tel-input, you can either not use
// a default country, or provide the default country as a 2nd argument to the
// parsing function - parsePhoneNumberFromString(this.phone, this.defaultCountry)
const result = parsePhoneNumberFromString((this.$refs.tel || {})._data.phone);
return result.country;
}
}不要忘记将包添加到您的项目npm install libphonenumber-js中。
发布于 2021-01-27 16:45:18
您只需创建一个具有每个国家代码和数字前缀的对象,如下所示:
country_codes = {
'+91': 'IN',
'+46': 'SE',
...
}以及使用this to“转换”数字前缀为两位数的国家代码。
country_codes['+46'] //returns 'SE'您可以在此处找到所有国家/地区代码:https://countrycode.org/
https://stackoverflow.com/questions/65915530
复制相似问题