如何使用vue-json-viewer插件展示JSON格式數(shù)據(jù)
1、下載 vue-json-viewer
npm
下載 vue-json-viewer
:
// Vue2 npm install vue-json-viewer@2 --save // Vue3 npm install vue-json-viewer@3 --save
yarn
下載 vue-json-viewer
:
// Vue2 yarn add vue-json-viewer@2 // Vue3 yarn add vue-json-viewer@3
2、引入插件并注冊(cè)
引入插件有兩種方式:可以全局引入,也可以在單個(gè)頁面文件中引入該插件。
2.1、全局注冊(cè)組件
如果在全局 main.js
中引入,那么全局可用,無需在單獨(dú)頁面的 components
中注入 JsonViewer
,可直接調(diào)用組件。具體實(shí)現(xiàn):
在 main.js
文件中添加:
import JsonViewer from 'vue-json-viewer' Vue.use(JsonViewer)
2.2、局部引入
如果在單頁面中只需要引入 import JsonViewer from 'vue-json-viewer'
,然后在 components
中注入 JsonViewer
組件,即可正常使用。
具體實(shí)現(xiàn):
// vue單頁面文件中引入 <script> // 引入組件 import JsonViewer from 'vue-json-viewer' export default { // 注冊(cè)組件 components:{ JsonViewer } } </script>
3、封裝組件 JsonView
新建 JsonView.vue
文件:
<template> <div> <div v-show="showJson"> <vue-json-viewer :value="jsonData" :expand-depth="expand"></vue-json-viewer> </div> <div v-show="!showJson"> {{data}} </div> </div> </template> <script> import vueJsonViewer from 'vue-json-viewer' export default { name: '', data () { return { jsonData: this.data } }, components: { vueJsonViewer }, props: { showJson: { type: Boolean, default: true }, data: { type: Object, default: () => { return {} } }, // 展開層數(shù) expand: { type: Number, default: 2 } }, watch: { data () { this.jsonData = this.data } }, computed: { }, created () { }, methods: { } } </script> <style lang='less' module> </style>
value
代表顯示的 JSON
數(shù)據(jù)。
4、組件內(nèi)使用
在需要使用的頁面:
<template> <div> <json-view :data="jsonData"></json-view> </div> </template> <script> import jsonView from './components/JsonView.vue' export default { name: '', components: { jsonView }, data () { return { jsonData: { "name": "小明", "age": 24, "gender": true, "height": 1.85, "weight": null, "skills": [ { "PHP": [ "Laravel", "Composer" ] }, { "JavaScript": [ "jQuery", "Vue", "React" ] }, "Golang", "Python", "Lua" ] } } }, methods: { } } </script> <style lang=''> </style>
頁面效果:
5、插件可選配置說明
屬性 | 說明 | 類型 | 默認(rèn)值 |
---|---|---|---|
json | 傳入的json數(shù)據(jù)(必填) | Object | - |
closed | 是否折疊全部 | Boolean | false |
deep | 展開深度,越大渲染速度越慢,建議不超過5) | Number | 3 |
icon-style | 折疊按鈕樣式,可選值為 [square, circle, triangle ] | String | square |
icon-color | 兩個(gè)折疊按鈕的顏色 | Array | theme = vs-code 時(shí),[’#c6c6c6’, ‘#c6c6c6’],其他情況為 [’#747983’, ‘#747983’] |
theme | 可選主題樣式,可選值為 [one-dark, vs-code], 不選時(shí)為默認(rèn)的白色主題 | String | - |
font-size | 字體大小,單位 px | Number | 14 |
line-height | 行高,單位 px | Number | 24 |
注:行高和字體大小不建議選用過大值,因?yàn)?nbsp;icon 大小、每行的 padding-left 等參數(shù)并不會(huì)隨之發(fā)生改變。
總結(jié)
到此這篇關(guān)于如何使用vue-json-viewer插件展示JSON格式數(shù)據(jù)的文章就介紹到這了,更多相關(guān)vue-json-viewer展示JSON數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一次前端Vue項(xiàng)目國(guó)際化解決方案的實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于前端Vue項(xiàng)目國(guó)際化解決方案的實(shí)戰(zhàn)記錄,以上只是一部分Vue項(xiàng)目開發(fā)中遇到的典型問題和解決方案,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07Vue3響應(yīng)式高階用法之markRaw()的使用
在Vue3中,markRaw()用于防止對(duì)象被轉(zhuǎn)換為響應(yīng)式,適用于管理大型庫(kù)對(duì)象或靜態(tài)數(shù)據(jù),有助于優(yōu)化性能和防止不必要的修改,本文就來詳細(xì)的介紹一下,感興趣的可以了解一下2024-09-09VUE使用 wx-open-launch-app 組件開發(fā)微信打開APP功能
這篇文章主要介紹了VUE使用 wx-open-launch-app 組件開發(fā)微信打開APP功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08vxe-table 實(shí)現(xiàn)行高拖拽功能示例詳解
vxe-table實(shí)現(xiàn)行高拖拽功能,需更新到最新版本,通過row-config.resizable和row-resize啟用,多列允許觸發(fā)行高拖拽時(shí),通過row-resize指定任意列,感興趣的朋友跟隨小編一起看看吧2025-01-01