当前位置:   article > 正文

vue3+json-editor-vue3实现JSON编辑器

json-editor-vue3

json-editor-vue3 GitHub官网:GitHub - guyue88/json-editor-vue3

一. 介绍

基于 jsoneditor 开发的vue3 json editor,支持全屏编辑,有完善的事件回调,可以在失去焦点时,对编辑器内容做校验。

二. 安装

npm install json-editor-vue3

三. 全局安装

  1. import Vue from 'vue'
  2. import JsonEditorVue from 'json-editor-vue3'
  3. Vue.use(JsonEditorVue)

四. 关于:vue3+ts无法渲染,报错 jsoneditor does not provide an export named 'default'

1. 安装插件 @originjs/vite-plugin-commonjs
npm install @originjs/vite-plugin-commonjs
2. vite.config.js 内
  1. // vite.config.js
  2. import { viteCommonjs } from '@originjs/vite-plugin-commonjs'
  3. export default defineConfig({
  4. plugins: [vue(),viteCommonjs()],
  5. })

五. 完整项目代码(将代码中接口相关代码替换成自己的!!!

  1. <script lang="ts" setup>
  2. import { ref, onMounted } from "vue";
  3. import { message } from "ant-design-vue";
  4. // 数据源
  5. const data = ref();
  6. // 是否存在错误
  7. const errors = ref(0);
  8. // 错误行数
  9. const line = ref();
  10. // 获取 接口字段
  11. const classify = ref("ABC");
  12. // 修改接口 字段
  13. const setJson = ref({
  14. classify: "",
  15. jsonData: "",
  16. });
  17. // 获取JSON
  18. const getJson = async () => {
  19. const res = await 获取JSON数据接口;
  20. data.value = res.data;
  21. };
  22. getJson();
  23. // 数据更新时触发
  24. const updataModel = (val: any) => {
  25. data.value = val;
  26. };
  27. // 数据错误时触发
  28. const editError = (a: any, error: any) => {
  29. errors.value = error.length;
  30. if (error[0]) {
  31. line.value = error[0].line;
  32. }
  33. };
  34. // 保存修改
  35. const btn_save = async () => {
  36. if (errors.value == 0) {
  37. const toJson = JSON.stringify(data.value);
  38. setJson.value.classify = classify.value;
  39. setJson.value.jsonData = toJson;
  40. await 更改JSON数据接口;
  41. message.success("保存成功");
  42. } else {
  43. message.error("保存失败,第 " + line.value + " 行存在格式错误");
  44. }
  45. };
  46. // 重置内容
  47. const btn_reload = () => {
  48. getJson();
  49. };
  50. // CDE按钮
  51. const get_Json = (type: any) => {
  52. if (type == "ABC") {
  53. classify.value = type;
  54. }
  55. if (type == "TASk") {
  56. classify.value = type;
  57. }
  58. if (type == "SYSTEM") {
  59. classify.value = type;
  60. }
  61. if (type == "REBAR") {
  62. classify.value = type;
  63. }
  64. if (type == "DICT") {
  65. classify.value = type;
  66. }
  67. getJson();
  68. };
  69. onMounted(() => {
  70. // 删除右上角logo和全屏按钮
  71. const a = document.getElementsByClassName("jsoneditor-poweredBy");
  72. const full = document.getElementsByClassName("full-screen");
  73. const aArray = Array.from(a);
  74. const fullArray = Array.from(full);
  75. // 遍历数组并移除每个元素
  76. aArray.forEach((element) => {
  77. if (element.parentNode) {
  78. element.parentNode.removeChild(element);
  79. }
  80. });
  81. fullArray.forEach((element) => {
  82. if (element.parentNode) {
  83. element.parentNode.removeChild(element);
  84. }
  85. });
  86. });
  87. </script>
  88. <template>
  89. <div>
  90. <div class="btn_style">
  91. <div class="btn_left">
  92. <!-- 更换不同数据源(带有选中效果) -->
  93. <a-button
  94. :type="classify === 'ABC' ? 'primary' : 'dashed'"
  95. @click="get_Json('ABC')"
  96. >Abc</a-button
  97. >
  98. <a-button
  99. :type="classify === 'TASk' ? 'primary' : 'dashed'"
  100. @click="get_Json('TASk')"
  101. >Task</a-button
  102. >
  103. <a-button
  104. :type="classify === 'SYSTEM' ? 'primary' : 'dashed'"
  105. @click="get_Json('SYSTEM')"
  106. >System</a-button
  107. >
  108. <a-button
  109. :type="classify === 'REBAR' ? 'primary' : 'dashed'"
  110. @click="get_Json('REBAR')"
  111. >REBAR</a-button
  112. >
  113. <a-button
  114. :type="classify === 'DICT' ? 'primary' : 'dashed'"
  115. @click="get_Json('DICT')"
  116. >DICT</a-button
  117. >
  118. </div>
  119. <div class="btn_right">
  120. <a-button danger ghost @click="btn_reload">复原</a-button>
  121. <a-button type="primary" ghost @click="btn_save">保存</a-button>
  122. </div>
  123. </div>
  124. <json-editor-vue
  125. @update:modelValue="updataModel"
  126. @validationError="editError"
  127. class="editor"
  128. v-model="data"
  129. style="height: 94vh; position: relative"
  130. />
  131. </div>
  132. </template>
  133. <style scoped>
  134. .btn_save {
  135. position: absolute;
  136. top: 0;
  137. right: 0;
  138. }
  139. .btn_reload {
  140. position: absolute;
  141. top: 0;
  142. right: 60px;
  143. }
  144. .btn_style {
  145. display: flex;
  146. width: 100%;
  147. justify-content: space-between;
  148. padding: 10px 16px 16px;
  149. box-sizing: border-box;
  150. height: 50px;
  151. }
  152. .btn_left {
  153. width: 380px;
  154. display: flex;
  155. justify-content: space-between;
  156. }
  157. .btn_right {
  158. width: 140px;
  159. display: flex;
  160. justify-content: space-between;
  161. }
  162. </style>

注:本项目中,按钮样式、成功失败提示,使用 Ant-Design-Vue插件,按需安装插件。官方网址:Ant Design Vue — An enterprise-class UI components based on Ant Design and Vue.js

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/891404
推荐阅读
相关标签
  

闽ICP备14008679号