首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有国家选择器的电话号码输入

没有国家选择器的电话号码输入
EN

Stack Overflow用户
提问于 2021-06-29 06:07:41
回答 2查看 1.7K关注 0票数 3

我正在尝试使用流行的反应-电话号码输入包,但是,在使用with country选项时,我发现了各种类型记录错误,目前为止所做的一切都是找到的:

代码语言:javascript
复制
npm install --save react-phone-number-input
npm install --save @types/react-phone-number-input


import PhoneInput from 'react-phone-number-input'

<PhoneInput/>

但是,对于without country,我们必须这样做,就像他们在网站上指出的那样:

代码语言:javascript
复制
import PhoneInput from 'react-phone-number-input/input'

function Example() {
  // `value` will be the parsed phone number in E.164 format.
  // Example: "+12133734253".
  const [value, setValue] = useState()
  // If `country` property is not passed
  // then "International" format is used.
  // Otherwise, "National" format is used.
  return (
    <PhoneInput
      country="US"
      value={value}
      onChange={setValue} />
  )
}

但是,`react-phone-number-input/input.的问题在于类型记录的抱怨,而且他们似乎忘了提到@的问题

这是我得到的具体错误:

代码语言:javascript
复制
    TS7016: Could not find a declaration file for module 'react-phone-number-input/input'. 
'/home/liz/prj/node_modules/react-phone-number-input/input/index.commonjs.js' implicitly has an 'any' type.   
Try `npm install @types/react-phone-number-input-input-min` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-phone-number-input/input';

我该如何解决这个问题?文档中提到的任何解决方案或安装?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-29 16:59:49

创建自定义类型declaration.Steps:

  1. 创建名为“react电话号码-输入”的文件夹
  2. 创建子文件夹输入
  3. 在其中创建index.d.ts
  4. 复制并粘贴以下代码

在这里,我只提供了一小部分类型,这些类型按照我的需求是足够的,但是它足以在此基础上构建:

代码语言:javascript
复制
/// <reference types="react">

declare module 'react-phone-number-input/input' {
  interface CustomPhoneInputProps {
    onChange: (value: string) => void;
    value: string;
    placeholder: string;
    className: string;
  }

  export default class PhoneInput extends React.Component<CustomPhoneInputProps, object> {}
}
票数 2
EN

Stack Overflow用户

发布于 2022-01-31 20:54:33

我认为这是他们的指示中的一个错误。从他们的演示来看,应该是

代码语言:javascript
复制
import Input from 'react-phone-number-input/input'

<Input
  placeholder="Enter phone number"
  value={value}
  onChange={setValue} />

注意,它是输入,而不是PhoneInput。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68173421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档