当前位置:   article > 正文

vue3 JSON editor 基于vanilla-jsoneditor

vanilla-jsoneditor
  1. <template>
  2. <div ref="jsonEditor" style="width: 100%"></div>
  3. </template>
  4. <script setup lang="ts" name="JsonEditor">
  5. import { ref, onMounted, watch } from "vue";
  6. import { JSONEditor, Mode } from "vanilla-jsoneditor";
  7. import type { MenuItem } from "vanilla-jsoneditor";
  8. const props = defineProps({
  9. modelValue: {
  10. type: Object,
  11. default() {
  12. return {};
  13. }
  14. },
  15. initJson: {
  16. type: Object,
  17. default() {
  18. return {};
  19. }
  20. },
  21. readOnly: {
  22. type: Boolean,
  23. default: false
  24. }
  25. });
  26. const emit = defineEmits(["update:modelValue"]);
  27. const jsonEditor = ref(null);
  28. let jsonInstance: any;
  29. watch(
  30. () => {
  31. return props.initJson;
  32. },
  33. () => {
  34. jsonInstance?.set({
  35. json: props.initJson
  36. });
  37. },
  38. {
  39. immediate: true
  40. }
  41. );
  42. onMounted(() => {
  43. init();
  44. });
  45. const init = () => {
  46. jsonInstance = new JSONEditor({
  47. target: jsonEditor.value as any,
  48. props: {
  49. content: {
  50. json: props.modelValue
  51. },
  52. readOnly: props.readOnly,
  53. mainMenuBar: true,
  54. statusBar: false,
  55. onRenderMenu: function (items: MenuItem[]) {
  56. return [items[0], items[3], items[4], items[5]];
  57. },
  58. mode: Mode["text"],
  59. onChange: (updatedContent: any, _previousContent: any, { contentErrors }) => {
  60. if (!contentErrors) {
  61. emit("update:modelValue", JSON.parse(updatedContent.text));
  62. }
  63. }
  64. }
  65. });
  66. };
  67. </script>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号