【问题标题】:How to import style properties from a .json document into a react component?如何将 .json 文档中的样式属性导入反应组件?
【发布时间】:2021-04-23 09:43:57
【问题描述】:

我想做的是将宽度、高度和样式属性导入反应组件。我将首先显示带有对象属性的 .json 文档:

            "manufacturerData": {
                "name": "Duesenberg",
                "articleCount": 0,
                "image": {
                    "file": "duesenberg.jpg",
                    "width": "140",
                    "height": "52",
                    "exists": true
                }
            },

我想将样式:widthheight 导入到我的图片卡片容器中,如下所示:

  return (
    <Card className='my-3 p-3 thumbnail' style={{ width: '14rem' }}>
      <a href={`/articles/${articles.id}`}>
        <Card.Img src={`/images/${articles.image.file}`} variat='top' />
      </a>
    </Card>
  )

我似乎无法在网上找到任何关于如何完成这项工作的直接说明。 非常感谢您的帮助。

【问题讨论】:

  • 谢谢你,克里斯,它工作得很好,路径让我感到困惑,但你是对的。

标签: javascript html css reactjs


【解决方案1】:
  return (
    // by default 'px' units will be assumed. You can add any other unit explicitly
    <Card className='my-3 p-3 thumbnail' style={{ width: articles.image.width }}>
   // <Card className='my-3 p-3 thumbnail' style={{ width: `${articles.image.width}rem` }}>
      <a href={`/articles/${articles.id}`}>
        <Card.Img src={`/images/${articles.image.file}`} variat='top' />
      </a>
    </Card>
  )

【讨论】:

  • 谢谢阿努拉格,好人,背部抽搐让我头疼。得到了我的风格 - 感谢帮助人:0)
猜你喜欢
  • 2018-11-09
  • 2018-11-24
  • 2017-07-13
  • 2021-03-12
  • 2020-05-09
  • 2017-04-19
  • 1970-01-01
  • 2018-02-04
  • 2021-06-09
相关资源
最近更新 更多