【问题标题】:Can Babel make Number.prototype.toLocaleString() work alike in React Native on all phones?Babel 能否让 Number.prototype.toLocaleString() 在所有手机上的 React Native 中都一样工作?
【发布时间】:2018-03-05 07:25:37
【问题描述】:

我在我的第一个 React Native 应用程序中使用 Number.prototype.toLocaleString() 来获得舍入和千位分隔符与世界货币的良好配合。

它在 iOS 上运行良好,但在 Android 上却不行。我猜不同的 JS 引擎对toLocaleString 的支持不同。

我了解 BabelJS 支持 polyfill,但不完全了解它的作用和未涵盖的内容,尤其是找不到有关此功能的信息。

有没有办法让它在 RN 中使用 Babl 来填充它?

【问题讨论】:

    标签: javascript react-native babeljs polyfills


    【解决方案1】:

    查找Number#toLocaleString()的polyfill,我找到了this module,它不需要使用babel。为了在您的代码中激活它,请在需要之前将此行放在某处:

    // recommended
    require('number-tolocalestring-polyfill')()
    

    如果你需要强制 polyfill 加载,即使它检测到环境已经支持Number#toLocaleString() 你也可以这样做:

    // probably for debug purposes only
    require('number-tolocalestring-polyfill')(true)
    

    【讨论】:

    • 就我而言,似乎两个平台都支持Number#toLocaleString,但并非所有功能。我会试一试-谢谢!查看它的源代码,我看不出它实际上支持哪些功能。它明确不支持货币。它似乎不支持useGrouping 用于数千个分隔符,我正在使用:runkit.com/embed/yhv29j5sybvn
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 2016-10-02
    • 2017-01-20
    相关资源
    最近更新 更多