json-editor.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <script setup lang="ts">
  2. import { reactive, watch } from "vue";
  3. import "vue-json-pretty/lib/styles.css";
  4. import VueJsonPretty from "vue-json-pretty";
  5. defineOptions({
  6. name: "JsonEditor"
  7. });
  8. const defaultData = {
  9. status: 200,
  10. text: "",
  11. error: null,
  12. config: undefined,
  13. data: [
  14. {
  15. news_id: 51184,
  16. title: "iPhone X Review: Innovative future with real black technology",
  17. source: "Netease phone"
  18. },
  19. {
  20. news_id: 51183,
  21. title:
  22. "Traffic paradise: How to design streets for people and unmanned vehicles in the future?",
  23. source: "Netease smart",
  24. link: "http://netease.smart/traffic-paradise/1235"
  25. },
  26. {
  27. news_id: 51182,
  28. title:
  29. "Teslamask's American Business Relations: The government does not pay billions to build factories",
  30. source: "AI Finance",
  31. members: ["Daniel", "Mike", "John"]
  32. }
  33. ]
  34. };
  35. const state = reactive({
  36. val: JSON.stringify(defaultData),
  37. data: defaultData,
  38. showLine: true,
  39. showLineNumber: true,
  40. showDoubleQuotes: true,
  41. showLength: true,
  42. editable: true,
  43. showIcon: true,
  44. editableTrigger: "click",
  45. deep: 3
  46. });
  47. watch(
  48. () => state.val,
  49. newVal => {
  50. try {
  51. state.data = JSON.parse(newVal);
  52. } catch (err) {
  53. // console.log('JSON ERROR');
  54. }
  55. }
  56. );
  57. watch(
  58. () => state.data,
  59. newVal => {
  60. try {
  61. state.val = JSON.stringify(newVal);
  62. } catch (err) {
  63. // console.log('JSON ERROR');
  64. }
  65. }
  66. );
  67. </script>
  68. <template>
  69. <el-card shadow="never">
  70. <template #header>
  71. <div class="card-header">
  72. <span class="font-medium">
  73. JSON编辑器,采用开源的
  74. <el-link
  75. href="https://github.com/leezng/vue-json-pretty"
  76. target="_blank"
  77. style="margin: 0 4px 5px; font-size: 16px"
  78. >
  79. vue-json-pretty
  80. </el-link>
  81. (支持大数据量)。
  82. </span>
  83. <span class="font-medium">
  84. 当然还有一款代码编辑器推荐(这里就不做演示了),采用开源的
  85. <el-link
  86. href="https://github.com/surmon-china/vue-codemirror"
  87. target="_blank"
  88. style="margin: 0 4px 5px; font-size: 16px"
  89. >
  90. codemirror6
  91. </el-link>
  92. </span>
  93. </div>
  94. <el-link
  95. class="mt-2"
  96. href="https://github.com/pure-admin/vue-pure-admin/blob/main/src/views/components/json-editor.vue"
  97. target="_blank"
  98. >
  99. 代码位置 src/views/components/json-editor.vue
  100. </el-link>
  101. </template>
  102. <vue-json-pretty
  103. v-model:data="state.data"
  104. :deep="state.deep"
  105. :show-double-quotes="state.showDoubleQuotes"
  106. :show-line="state.showLine"
  107. :show-length="state.showLength"
  108. :show-icon="state.showIcon"
  109. :show-line-number="state.showLineNumber"
  110. :editable="state.editable"
  111. :editable-trigger="state.editableTrigger as any"
  112. />
  113. </el-card>
  114. </template>