在这里插入图片描述

npm install vue-json-viewer --save
 <el-button type="primary" @click="previewClick">预览</el-button>
 
 <el-dialog title="预览" :visible.sync="previewVisible" width="70%">
      <viewer ref="viewer" style="height: 500px"></viewer>
    </el-dialog>
    
import viewer from './components/viewer.vue'
export default {
  components: { viewer },
  methods:{
    previewClick(){
         this.previewVisible = true
         let list = [
        {
            "id": "1",
            "amount": 1,
            "count": 1,
            "label": "a"
        },
        {
            "id": "2",
            "amount": 2,
            "count": 2,
            "label": "b"
        },
        ]
          const str = JSON.stringify(list)
          this.$nextTick(() => {
            this.$refs.viewer.jsonStr = str
          })
    }
  }
}



viewer.vue

<template>
  <div class="codeEditBox">
    <json-viewer
      :value="JSON.parse(jsonStr)"
      :expand-depth="5"
      boxed
      sort
      :show-array-index="false"
      copyable
    >
      <template slot="copy">
        <i class="el-icon-document-copy" title="复制"></i>
      </template>
    </json-viewer>
  </div>
</template>
<script>
import JsonViewer from 'vue-json-viewer'
export default {
  components: {
    JsonViewer,
  },
  data() {
    return {
      jsonStr: '',
    }
  },
  methods: {},
}
</script>
<style scoped>
.codeEditBox {
  width: 100%;
  height: 200px;
  border: 1px solid #dcdee2;
  overflow-y: auto;
}
</style>

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐