【问题标题】:Why/How does this code works in ES6 (React) [duplicate]为什么/如何在 ES6 (React) 中运行此代码 [重复]
【发布时间】:2017-10-27 15:57:28
【问题描述】:

我正在学习 React 课程,遇到了这节课,他们从 styled-components 导入了一个组件,但对我来说奇怪的是第 4 行,他们使用反引号插值而不将其括在括号中,似乎不应该是反引号和styled.header 之间的任何空格。

【问题讨论】:

  • var test = (a) => console.log(a+' man'); test`whatever` 将输出whatever man。在您的示例中,styled.header 是一个接收字符串(在您的示例中为 css)作为其唯一参数的函数。
  • 附带问题:您使用什么 IDE 可以在字符串模板文字中正确突出显示 CSS?它是否也为您提供代码完成功能,例如 text-align: 建议 left, right, center 等? (在我的 IDE 中缺少它是我不喜欢 styled-components 的原因之一。)
  • @Aaron 它是 Atom 的语言包,更多信息在这里github.com/styled-components/…

标签: reactjs ecmascript-6 backticks styled-components


【解决方案1】:

这就是 Tagged Template Literals,这是 ES6 中的一项新功能。

您可以在the styled-components docsarticle 中阅读更多相关信息。

【讨论】:

    猜你喜欢
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多