本文介绍了为什么Intl.NumberFormat无法在Safari和Firefox中与单元一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用给出的示例此处有关格式化数字.

I'm trying to use the example given here concerning formatting a number.

console.log((16).toLocaleString('en-GB', {
    style: "unit",
    unit: "liter",
    unitDisplay: "long"
}));

我在chrome中尝试过,效果很好.但是,在Safari和Firefox中,我分别收到以下错误Error: style must be either "decimal", "percent", or "currency"Error: invalid value "unit" for option style.

I tried it in chrome and it works great. However, in Safari as well as Firefox I get the following errors respectively Error: style must be either "decimal", "percent", or "currency" and Error: invalid value "unit" for option style.

从文档中我发现,在查看浏览器兼容性之后,它应该可以在所有浏览器上运行.我试图寻找答案,但是找不到关于此问题的任何信息.有谁知道这是为什么,或者我可能在哪里找到更多信息?

From the docs I figured it should work on all browsers after looking at the browser compatibility. I tried looking for answers, but I can't find anything regarding this issue. Does anyone know why this is or where I could probably find more info?

推荐答案

问题来自style字段的值unit.

根据 ECMA-402,第六版,2019年6月ECMAScript®2019国际化API规范:

According to ECMA-402, 6th edition, June 2019ECMAScript® 2019 Internationalization API Specification:

根据 ECMA-402草案/2020年2月27日ECMAScript®2020国际化API规范:

According to the Draft ECMA-402 / February 27, 2020ECMAScript® 2020 Internationalization API Specification:

Firefox和Safari正在实施ECMA-402规范的第六版,而Chrome浏览器正在实施同一规范的草稿版本.规范草案可以随时更改,并且没有正式保证该新的unit值将包含在第7版中.如果您想跨浏览器兼容并且面向未来,则应坚持使用第6版,并等待第7版发布,然后再使用这些新功能.

Firefox and Safari are implementing the 6th edition of the ECMA-402 specification and Chrome is implementing the Draft version of this same specification. The draft specification can change at any time and there is no formal guarantees that this new unit value will be included in the 7th edition. If you want to be cross-browser compatible and future-proof, you should stick on the 6th edition and wait for the release of the 7th edition before using these new features.

如果您想了解详细信息,可以阅读此新功能的建议

If you want details you can read the proposition for this new feature.

这篇关于为什么Intl.NumberFormat无法在Safari和Firefox中与单元一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 05:17