将数组数组作为道具传递

Posted

技术标签:

【中文标题】将数组数组作为道具传递【英文标题】:Passing array of arrays as props 【发布时间】:2020-07-12 18:16:08 【问题描述】:

谁能解释我在这里做错了什么......

<Something
content=[
    
        heading: "Add Company",
        icon: "plus",
        options: [
            
                image: "srcimage",
                text: "New Company",
                link: "test"
            ,   //Errors here
                image: "srcimage",
                text: "Existing Company",
                link: "test"
            
        ]
    , 
        heading: "Import Company",
        icon: "download",
        options: [
            
                image: "/src/image",
                text: "Class",
                link: "/test"
            ,
            
                image: "/src/image",
                text: "BGL",
                link: "/test"
            ,
            
                image: "/src/image",
                text: "SuperMate",
                link: "/test"
            
        ]
    ]
 />

我收到错误消息...Unexpected token, expected "]",上面写着error here。最终,我想根据这样传入的内容创建一些内容块......

谢谢

【问题讨论】:

【参考方案1】:

[] 不是对象的有效语法。

content=[
  
    heading: "Add Company",
    icon: "plus",
    options: [ // remove the curly boy that was here
      
        image: "srcimage",
        text: "New Company",
        link: "test"
      ,  
        image: "srcimage",
        text: "Existing Company",
        link: "test"
      
    ] // and here
  , 
    heading: "Import Company",
    icon: "download",
    options: [ // and here
      
        image: "/src/image",
        text: "Class",
        link: "/test"
      ,
      
        image: "/src/image",
        text: "BGL",
        link: "/test"
      ,
      
        image: "/src/image",
        text: "SuperMate",
        link: "/test"
      
    ] // and here
  ]

【讨论】:

【参考方案2】:

对于你需要去掉花括号的选项属性,只需传递数组

options: [
  
    image: "srcimage",
    text: "New Company",
    link: "test"
  , 
  
    image: "srcimage",
    text: "Existing Company",
    link: "test"
  
]

【讨论】:

以上是关于将数组数组作为道具传递的主要内容,如果未能解决你的问题,请参考以下文章

为啥数组中的两个对象中只有一个作为道具传递?

如何在vue中将不同值的数组作为道具传递

传递颜色作为道具

反应:作为道具的数组显示为未定义

将道具(数组)传递到路由器链接路径

如何将从父组件传递的道具推送到子组件中的数组?