【问题标题】:can't use replaceAll or replace with nextjs/reactjs不能使用 replaceAll 或替换为 nextjs/reactjs
【发布时间】:2020-11-06 11:45:36
【问题描述】:

我正在使用 reactjs/nextjs,我目前正在尝试渲染这个组件:

const Example = ({data}) => (
<strong>{() => data.slug.replaceAll("-", " ")}</strong>);

我每次都会收到此错误:
TypeError: data.slug.replaceAll is not a function

slug 的类型是一个字符串,我检查了它。
我尝试使用在另一个 stackoverflow 答案中找到的data.slug.toString().replaceAll...,但我遇到了同样的问题。

我尝试使用函数而不是直接字符串,即:() =&gt; data.slug.replaceAll...,这次没有渲染。

我不知道它是否会有所帮助,但我尝试渲染的数据来自一个 api 通过getServerSideProps 函数异步。

【问题讨论】:

    标签: javascript reactjs next.js


    【解决方案1】:

    很可能是因为.replaceAll() 尚不支持所有浏览器。

    caniuse

    暂时可以用简单的.replace()

    data.slug.replace(/-/g, " ")
    

    【讨论】:

    • 感谢您为我工作。这很奇怪,因为 replaceAll 在浏览器控制台中工作得很好。我认为这与 babel 有关。
    猜你喜欢
    • 2019-01-13
    • 2012-06-10
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多