【问题标题】:Issue with number-formatting数字格式问题
【发布时间】:2018-07-14 09:22:14
【问题描述】:

我想使用libphonenumber library 将带有给定国家代码(如"US")的电话号码(如2133734253)格式化为国家格式。

我在文档中发现的内容类似于:


因为我没有使用node.js 或其他东西,所以我只是从https://unpkg.com/libphonenumber-js/bundle/libphonenumber-js.min.js 链接js 文件。

使用new libphonenumber.formatNumber()-call 似乎不起作用,因为它每次都返回一个绝对没有内容的对象。我怎样才能解决这个问题?如何正确使用formatNumber()

let phonenumber = new libphonenumber.formatNumber({ 
                                      country: 'US', 
                                      phone: '2133734253' 
                                    }, 'National');
console.log(phonenumber) // should be (213) 373-4253
<script src="https://unpkg.com/libphonenumber-js/bundle/libphonenumber-js.min.js"></script>

【问题讨论】:

    标签: javascript html phone-number libphonenumber


    【解决方案1】:

    根据文档,libphonenumber 不是构造函数,它只是提供函数,因此删除 new 关键字:

    let phonenumber = libphonenumber.formatNumber({ 
                                          country: 'US', 
                                          phone: '2133734253' 
                                        }, 'National');
    console.log(phonenumber) // should be (213) 373-4253
    <script src="https://unpkg.com/libphonenumber-js/bundle/libphonenumber-js.min.js"></script>

    【讨论】:

      【解决方案2】:

      您所要做的就是删除new。这里不需要,它是一个utils库,不需要实例化:

      let phonenumber = libphonenumber.formatNumber({ 
                                            country: 'US', 
                                            phone: '2133734253' 
                                          }, 'National');
      console.log(phonenumber) // should be (213) 373-4253
      <script src="https://unpkg.com/libphonenumber-js/bundle/libphonenumber-js.min.js"></script>

      【讨论】:

        猜你喜欢
        • 2023-03-27
        • 1970-01-01
        • 2013-12-05
        • 2021-01-02
        • 2014-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-16
        相关资源
        最近更新 更多