Vue中使用jsonView组件展示JSON数据方法详解

爱懒懒的小景景 2024-12-06 09:19:46编程技术
668

在现代 Web 开发中,JSON 数据的展示和调试是一个常见的需求。为了提高用户体验和开发效率,我们设计并实现了一个名为jsonView的 Vue 组件。这个组件不仅能够以可折叠的方式展示复杂的 JSON 数据,还提供了丰富的样式配置选项,使得 JSON 数据的呈现更加美观和易读。本文将详细介绍jsonView组件的功能和使用方法。

前两天干活儿有个需求,在前端需要展示可折叠的Json树,供开发人员查看,这里采用JsonView组件来实现,它是一款用于展示Json的Vue组件,支持大体积的Json文件快速解析渲染,下面记录一下实现过程。

1.首先先下载好JsonView的组件:JsonView.vue,组件代码如下:

<template>
 <div class="bgView">
  <div :class="['json-view', length ? 'closeable' : '']" :style="'font-size:' + fontSize+'px'">
  <span @click="toggleClose" :class="['angle', innerclosed ? 'closed' : '']" v-if="length">
  </span>
   <div class="content-wrap">
    <p class="first-line">
     <span v-if="jsonKey" class="json-key">"{{jsonKey}}": </span>
     <span v-if="length">
     {{prefix}}
     {{innerclosed ? ('...' + subfix) : ''}}
     <span class="json-note">
      {{innerclosed ? (' // count: ' + length) : ''}}
     </span>
    </span>
     <span v-if="!length">{{isArray ? '[]' : '{}'}}</span>
    </p>
    <div v-if="!innerclosed && length" class="json-body">
     <template v-for="(item, index) in items">
      <json-view :closed="closed" v-if="item.isJSON" :key="index" :json="item.value"
            :jsonKey="item.key" :isLast="index === items.length - 1"></json-view>
      <p class="json-item" v-else :key="index">
      <span class="json-key">
        {{(isArray ? '' : '"' + item.key + '"')}}
      </span>
       :
       <span class="json-value">
        {{item.value + (index === items.length - 1 ? '' : ',')}}
      </span>
      </p>
     </template>
     <span v-show="!innerclosed" class="body-line"></span>
    </div>
    <p v-if="!innerclosed && length" class="last-line">
     <span>{{subfix}}</span>
    </p>
   </div>
  </div>
 </div>
</template>

<script>
 export default {
  name: 'jsonView',
  props: {
   json: [Object, Array],
   jsonKey: {
    type: String,
    default: ''
   },
   closed: {
    type: Boolean,
    default: false
   },
   isLast: {
    type: Boolean,
    default: true
   },
   fontSize: {
    type: Number,
    default: 13
   }
  },
  created() {
   this.innerclosed = this.closed
   this.$watch('closed', () => {
    this.innerclosed = this.closed
   })
  },
  data() {
   return {
    innerclosed: true
   }
  },
  methods: {
   isObjectOrArray(source) {
    const type = Object.prototype.toString.call(source)
    const res = type === '[object Array]' || type === '[object Object]'
    return res
   },
   toggleClose() {
    if (this.innerclosed) {
     this.innerclosed = false
    } else {
     this.innerclosed = true
    }
   }
  },
  computed: {
   isArray() {
    return Object.prototype.toString.call(this.json) === '[object Array]'
   },
   length() {
    return this.isArray ? this.json.length : Object.keys(this.json).length
   },
   subfix() {
    return (this.isArray ? ']' : '}') + (this.isLast ? '' : ',')
   },
   prefix() {
    return this.isArray ? '[' : '{'
   },
   items() {
    if (this.isArray) {
     return this.json.map(item => {
      const isJSON = this.isObjectOrArray(item)
      return {
       value: isJSON ? item : JSON.stringify(item),
       isJSON,
       key: ''
      }
     })
    }
    const json = this.json
    return Object.keys(json).map(key => {
     const item = json[key]
     const isJSON = this.isObjectOrArray(item)
     return {
      value: isJSON ? item : JSON.stringify(item),
      isJSON,
      key
     }
    })
   }
  }
 }
</script>

<style>
 .bgView {
  background-color: #fafafa;
 }

 .json-view {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  white-space: nowrap;
  padding-left: 20px;
  box-sizing: border-box;
 }

 .json-note {
  color: #909399;
 }

 .json-key {
  color: rgb(147, 98, 15);
 }

 .json-value {
  color: rgb(24, 186, 24);
 }

 .json-item {
  margin: 0;
  padding-left: 20px;
 }

 .first-line {
  padding: 0;
  margin: 0;
 }

 .json-body {
  position: relative;
  padding: 0;
  margin: 0;
 }

 .json-body .body-line {
  position: absolute;
  height: 100%;
  width: 0;
  border-left: dashed 1px #bbb;
  top: 0;
  left: 2px;
 }

 .last-line {
  padding: 0;
  margin: 0;
 }

 .angle {
  position: absolute;
  display: block;
  cursor: pointer;
  float: left;
  width: 20px;
  text-align: center;
  left: 0;
 }

 .angle::after {
  content: "";
  display: inline-block;
  width: 0;
  height: 0;
  vertical-align: middle;
  border-top: solid 4px #333;
  border-left: solid 6px transparent;
  border-right: solid 6px transparent;
 }

 .angle.closed::after {
  border-left: solid 4px #333;
  border-top: solid 6px transparent;
  border-bottom: solid 6px transparent;
 }
</style>

2.在需要使用的vue页面中引用JsonView组件

import JsonView from '@/components/JsonView'

3.定义Json数据变量

jsonData:{},

4.页面展示代码

<JsonView :json="jsonData"></JsonView>

5.实现效果如下:

Vue中使用jsonView组件展示JSON数据方法详解

JsonViewAttributes

Vue中使用jsonView组件展示JSON数据方法详解

总结

jsonView组件通过其简洁而强大的功能,为开发者提供了一种高效且美观的方式来展示 JSON 数据。无论是简单的对象还是复杂的嵌套数组,jsonView都能轻松应对。通过灵活的属性配置和样式定义,开发者可以根据实际需求定制组件的外观和行为。无论是在前端调试工具中,还是在用户界面中,jsonView都是一个值得推荐的选择。希望本文的介绍能够帮助读者更好地理解和使用这一实用的 Vue 组件。

vue jsonview json
THE END
蜜芽
故事不长,也不难讲,四字概括,毫无意义。

相关推荐

Vue路由守卫中nextTick与next的作用与使用技巧详解
在Vue.js生态中,路由守卫和nextTick是控制导航流程与DOM更新时序的核心工具。路由守卫中的next函数决定了导航的走向,而nextTick则确保在DOM更新后执行关键操作。本文ZHANID...
2025-09-12 编程技术
517

Vue路由守卫是什么?带你了解Vue Router的导航控制机制
在单页应用(SPA)开发中,路由跳转的流畅性与安全性直接影响用户体验。Vue Router通过路由守卫(Route Guards)提供了一套完整的导航控制机制,允许开发者在路由切换的关键节...
2025-09-12 编程技术
500

VTJ.PRO:AI驱动的企业级低代码开发平台,让Vue3开发更高效
VTJ.PRO是一款AI驱动的企业级低代码开发平台,专注于前端开发领域,基于Vue3 + TypeScript + Vite构建,深度融合可视化设计、源码工程与AI智能引擎,旨在解决传统开发中的效率...
2025-09-11 新闻资讯
539

Vue watch结合axios实现数据联动教程:异步请求监听实战
在Vue开发中,数据的响应式更新是构建动态交互体验的核心。当数据变化需要触发异步请求时,watch 监听器结合 axios 就成为实现数据联动的利器。本文将通过实战案例,讲解如何...
2025-08-29 编程技术
468

Vant:有赞团队开源的移动端 Vue 组件库
Vant 是一个由有赞前端团队开源的移动端 Vue 组件库,目前已在 GitHub 上获得超过 20,000 颗星标,成为国内最受欢迎的 Vue 移动端组件库之一。该项目基于 Vue 3 构建,提供了...
2025-08-08 新闻资讯
697

Python文件读写操作技巧:高效处理文本与JSON
在Python编程中,文件读写是基础且至关重要的操作。本文ZHANID工具网将详细介绍Python中高效处理文本与JSON文件的读写操作技巧,帮助开发者在实际项目中更加得心应手。
2025-08-02 编程技术
478