细粒丁 2021-08-02 23:47 采纳率: 100%
浏览 457
已结题

Vue怎么动态插入html代码

举个例子:

    data: {
        detailBlock: [
            {
                label: "路径",
                attribute: "{{showMsg.path}}"
            }, {
                label: "名称",
                attribute: "{{showMsg.filename}}"
            }, {
                label: "标签",
                attribute: "{{showMsg.tag}}"
            }
        ],
        showMsg: {
            path: "path",
            filename: "filename",
            tag: "tag"
        },
        test: [
            {
                label: "路径",
                attribute: "showMsg.path"
            }, {
                label: "名称",
                attribute: "showMsg.filename"
            }, {
                label: "标签",
                attribute: "showMsg.tag"
            }
        ]
    },
  <div>
    <label v-for="(item,index) in detailBlock">
        {{item.attribute}}
    </label>
  </div>

就是我想让他显示的是:
path
filename
tag

但如果像现在这样写的话就显示:
{{showMsg.path}}
{{showMsg.filename}}
{{showMsg.tag}}

而且他又不能 {{ {{ ··· }} }}

这样写直接报错
  <div>
    <label v-for="(item,index) in test">
        {{ {{ item.attribute }} }}
    </label>
  </div>

试过用`$[···]也不管用

不知大家有什么好的建议……

  • 写回答

3条回答 默认 最新

  • 关注

    detailBlock改成

        detailBlock: [
            {
                label: "路径",
                attribute: "showMsg.path"
            }, {
                label: "名称",
                attribute: "showMsg.filename"
            }, {
                label: "标签",
                attribute: "showMsg.tag"
            }
        ],
    

    html是

      <div>
        <label v-for="(item,index) in detailBlock">
             {{ eval(item.attribute) }}
        </label>
      </div>
    

    或者detailBlock改成

        detailBlock: [
            {
                label: "路径",
                attribute: "path"
            }, {
                label: "名称",
                attribute: "filename"
            }, {
                label: "标签",
                attribute: "tag"
            }
        ],
    

    html是

      <div>
        <label v-for="(item,index) in detailBlock">
            {{showMsg[item.attribute]}}
        </label>
      </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月16日
  • 已采纳回答 8月8日
  • 创建了问题 8月2日

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题