组件代码(临时粘出来)
 <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: 18
}
},
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 lang="scss">
.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; .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; &::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;
}
&.closed::after {
border-left: solid 4px #333;
border-top: solid 6px transparent;
border-bottom: solid 6px transparent;
}
}
}
</style>
使用实例
<template>
<div>
<JsonView :json="JsonData"></JsonView>
</div>
</template> <script>
import JsonView from '@/components/JsonView' export default {
name: 'test',
data() {
return {
JsonData: {
'code': 200,
'message': 'succeed !',
'data': [
{
'uuid': '76254DDB-A8EA-46CB-B3D7-6EEBD13BB2E6',
'version': 1,
'code': '401',
'message': '请求无权限',
'createId': 'dev',
'createDate': '2018-12-03T00:00:00',
'modifyId': null,
'modifyDate': null
},
{
'uuid': 'B0415CC2-F0E0-4B0C-A3BA-50ABAEE98BB9',
'version': 1,
'code': '500',
'message': '服务器错误',
'createId': 'dev',
'createDate': '2018-12-03T00:00:00',
'modifyId': null,
'modifyDate': null
},
{
'uuid': 'B70692E0-CCB7-4C44-B59B-7B75B16FA9FE',
'version': 1,
'code': '200',
'message': '请求成功',
'createId': 'dev',
'createDate': '2018-12-03T15:06:54.717',
'modifyId': null,
'modifyDate': null
},
{
'uuid': 'C8A37C2D-0842-423B-AEBA-976C106A3E90',
'version': 1,
'code': '202',
'message': '请求失败',
'createId': 'dev',
'createDate': '2018-12-03T00:00:00',
'modifyId': null,
'modifyDate': null
}
]
}
}
},
components: { JsonView }
}
</script>

可传参数:

      json: [Object, Array], // 必传 显示的数据
closed: { // 是否默认展开
type: Boolean,
default: false
},
fontSize: { // 文字大小
type: Number,
default: 18
}

Vue JsonView 树形格式化代码插件的更多相关文章

  1. vscode格式化代码插件Beautify

    vscode格式化代码安装 VsCode 格式化代码插件搜索并安装 Beautify 格式化代码插件使用:打开要格式化的文件 —> F1 —> Beautify file —> 选择 ...

  2. sublime自动格式化代码插件HTML-CSS-JS Prettify安装

    sublime自动格式化代码插件HTML-CSS-JS Prettify安装 问题: 用 Sublime Text 格式化代码(安装 HTML-CSS-JS Prettify 插件)时,格式化时却会提 ...

  3. sublime text3安装格式化代码插件

    1.代码提示插件:sublimeCodeIntel a)在Sublime Text 3中,按下Ctrl+Shift+P调出命令面板;b)输入install 调出 Install Package 选项并 ...

  4. vsCode 设置vue 保存自动格式化代码

    setting { // vscode默认启用了根据文件类型自动设置tabsize的选项 "editor.detectIndentation": false, // 重新设定tab ...

  5. Qt Creator配置clang-format格式化代码插件

    clang-format是一种格式化代码的插件,可用于格式化C / C ++ / Java / JavaScript / Objective-C / Protobuf / C#代码.而Qt Creat ...

  6. UE编辑器加载格式化代码插件astyle

    UE 的格式化功能不强,自带的astyle版本陈旧,一般采用开源工具astyle来实现代码格式化. 1. 首先下载最新的astyle,因为ue自带的astyle版本太老,不支持空格.中文名等. 2. ...

  7. vscode vue eslint 快捷键格式化代码

    添加vetur , eslint插件   在工作区添加以下代码   "workbench.startupEditor": "welcomePage", &quo ...

  8. phpstorm通过FileWatchers配置自动格式化代码插件

    在自动格式代码的插件中, prettier一直是挺不错的, 这个插件在不同的IDE里有不同的配置地方, 但是配置参数基本上是差不多的. 下面就说明下在phpstorm(版本2019.2)中如何配置的吧 ...

  9. Sublime text 3 格式化代码 插件

    JsFormat: 重新打开sublime就能使用js格式化插件 使用方法: 1.快捷键:ctrl+alt+f 2.或者先用快捷键打开命令面板 “ctrl + shift + p”, 再输入 “For ...

随机推荐

  1. ISAP 算法的学习

    http://www.renfei.org/blog/isap.html 算法与数学 网络流-最大流问题 ISAP 算法解释 2013-08-07Renfei Song 2 条评论 内容提要 [隐藏] ...

  2. vs code--snippet与快速提示

    因为快速语法提示和建议冲突,所以要么禁用语法提示,要么禁用建议 Note that quick suggestions and Tab completion might interfere becau ...

  3. [bzoj1660][Usaco2006 Nov]Bad Hair Day_单调栈

    Bad Hair Day bzoj-1660 Usaco-2006 Nov 题目大意:n头牛站成一列,每头牛向后看.f[i]表示第i头牛到第n头牛之间有多少牛,使得这些牛都比i矮,且中间没有比i高的牛 ...

  4. C#版winform实现UrlEncode

    在Asp.net中可以使用Server.HTMLEncode和Server.URLEncode 将文本或URL的特殊字符编码,但在控制台或Winform程序中没有办法使用到这些方法, 解决办法:右击项 ...

  5. HDU 3389

    对于这道题,我们需要从(A+B)%3==0这式子考虑.对于第一条式子,我们可以知道,只能是奇偶盒子交替转移. 由第二条式子可知,要么是同余为0的A,B之间转移,要么是余数为1,2之间的 转移.后来仔细 ...

  6. HDOJ GCD 2588【欧拉函数】

    GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  7. 科普:alphago是什么

    鉴于大部分人类对alphago的认识: 1:Alphago有什么了不起的?不就是算得快吗.ibm早在20年前就通过象棋战胜人类了.又是Google的一次营销. 2:alphago 实现人工智能了,电脑 ...

  8. 什么是Spark?

    什么是Spark Spark是一个基于内存计算的开源的集群计算系统,目的是让数据分析更加高速.Spark很小巧玲珑,由加州伯克利大学AMP实验室的Matei为主的小团队所开发. 使用的语言是Scala ...

  9. c++11 实现半同步半异步线程池

    感受: 随着深入学习,现代c++给我带来越来越多的惊喜- c++真的变强大了. 半同步半异步线程池: 事实上非常好理解.分为三层 同步层:通过IO复用或者其它多线程多进程等不断的将待处理事件加入到队列 ...

  10. Sinowal Bootkit 分析-中国红客网络技术联盟 - Powered by Discuz!

    訪问原文 (一)模块组成         感染过Sinowal的电脑,Sinaowal在硬盘中的分布例如以下图: ; Sector                 Offset             ...