vue中json格式化顯示數(shù)據(jù)(vue-json-viewer)

1. 安裝
使用npm:
$ npm install vue-json-viewer@2 --save // Vue2 $ npm install vue-json-viewer@3 --save // Vue3
使用yarm:
$ yarn add vue-json-viewer@2 // Vue2 $ yarn add vue-json-viewer@3 // Vue3
使用pnpm也可以
2. 使用方法
在main.ts中注冊插件
import { createApp } from 'vue';
import JsonViewer from 'vue-json-viewer'
import App from './App.vue';
const app = createApp(App);
app.use(JsonViewer )
app.mount('#app');
在界面中使用
<template>
<json-viewer
:value="data"
:expand-depth="5"
copyable
boxed
sort
class="w-100%"></json-viewer>
</template>
<script setup lang="ts">
const data = {// 注意:綁定得數(shù)據(jù)一定是JSON,而不是JSON字符串
total: 25,
limit: 10,
data: [
{
id: '5968fcad629fa84ab65a5247',
firstname: 'Ada',
lastname: 'Lovelace',
},
],
};
</script>
注意:在main.ts文件中可能會出現(xiàn)以下得ts類型報錯:
無法找到模塊“vue-json-viewer”的聲明文件。

解決方法:
- 先嘗試安裝
npm i --save-dev @types/vue-json-viewer - 如果上面方法不行,在全局的
.d.ts結(jié)尾的文件中添加上declare module 'vue-json-viewer'
3. json-viewer屬性和方法
json-viewer屬性:

json-viewer事件監(jiān)聽:

json-viewer插槽:

json-viewer快捷鍵:

4. 表格中原始數(shù)據(jù)和格式化數(shù)據(jù)切換
實現(xiàn):給表格中每行數(shù)據(jù)單獨添加一個變量,用于單獨控制展示的方式。


//獲取到表格數(shù)據(jù)后,添加一個自定義變量,記錄json顯示的方式
state.tableData.forEach((item) => {
item.showJsonView = false;
});
<!--el-table-column 插槽中 -->
<template #default="scope">
<div v-if="col.key === 'accountConfig'">
<el-link
type="primary"
:underline="false"
@click="() => (scope.row.showJsonView = !scope.row.showJsonView)">
{{ scope.row.showJsonView ? '原始數(shù)據(jù)' : '格式化數(shù)據(jù)' }}
</el-link>
<div v-if="scope.row.showJsonView">
<json-viewer
:value="JSON.parse(scope.row[col.key])"
:expand-depth="1"
copyable></json-viewer>
</div>
<div v-else>
<pre class="text-pre-wrap">{{ scope.row[col.key] }}</pre>
</div>
</div>
<div v-else>
{{ scope.row[col.key] }}
</div>
</template>
注:如果表格中有el-switch, 單獨控制每行的操作,請求時顯示的loading,也是同樣的方法實現(xiàn)。
5. 自定義主題:
- 添加
theme="my-awesome-json-theme"到JsonViewer的組件屬性上 - 復(fù)制粘貼下面的模板并且根據(jù)自定義的theme名稱做對應(yīng)調(diào)整。
// values are default one from jv-light template
.my-awesome-json-theme {
background: #fff;
white-space: nowrap;
color: #525252;
font-size: 14px;
font-family: Consolas, Menlo, Courier, monospace;
.jv-ellipsis {
color: #999;
background-color: #eee;
display: inline-block;
line-height: 0.9;
font-size: 0.9em;
padding: 0px 4px 2px 4px;
border-radius: 3px;
vertical-align: 2px;
cursor: pointer;
user-select: none;
}
.jv-button { color: #49b3ff }
.jv-key { color: #111111 }
.jv-item {
&.jv-array { color: #111111 }
&.jv-boolean { color: #fc1e70 }
&.jv-function { color: #067bca }
&.jv-number { color: #fc1e70 }
&.jv-number-float { color: #fc1e70 }
&.jv-number-integer { color: #fc1e70 }
&.jv-object { color: #111111 }
&.jv-undefined { color: #e08331 }
&.jv-string {
color: #42b983;
word-break: break-word;
white-space: normal;
}
}
.jv-code {
.jv-toggle {
&:before {
padding: 0px 2px;
border-radius: 2px;
}
&:hover {
&:before {
background: #eee;
}
}
}
}
}總結(jié)
到此這篇關(guān)于vue中json格式化顯示數(shù)據(jù)(vue-json-viewer)的文章就介紹到這了,更多相關(guān)vue中json格式化顯示內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
element?table?數(shù)據(jù)量大頁面卡頓的解決
這篇文章主要介紹了element?table?數(shù)據(jù)量大頁面卡頓的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Vue3監(jiān)聽reactive對象中屬性變化的方法
在 Vue 3 中,如果你想監(jiān)聽 reactive 對象中的某個屬性發(fā)生的變化,你可以使用 watch 函數(shù)進(jìn)行監(jiān)聽,watch 函數(shù)允許你觀察 reactive 對象的某個屬性或者整個對象,所以本文給大家介紹了Vue3監(jiān)聽reactive對象中屬性變化的方法,需要的朋友可以參考下2024-08-08
vue項目element-ui級聯(lián)選擇器el-cascader回顯的問題及解決
這篇文章主要介紹了vue項目element-ui級聯(lián)選擇器el-cascader回顯的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
使用Vue3實現(xiàn)一個簡單的思維導(dǎo)圖組件
思維導(dǎo)圖是一種用于表示信息、想法和概念的圖形化工具,本文將基于 Vue3和VueDraggable實現(xiàn)一個簡單的思維導(dǎo)圖組件,支持節(jié)點拖拽,編輯及節(jié)點之間的關(guān)系連接,希望對大家有所幫助2025-04-04
vue使用axios實現(xiàn)動態(tài)追加數(shù)據(jù)

