vue3集成jsoneditor的方法详解

 更新时间:2023年09月07日 11:16:09   作者:修烛老司机  
JSONEditor是一个基于Web的工具,用于查看、编辑、格式化和验证JSON,它有各种模式,这篇文章主要为大家介绍了vue3集成jsoneditor的教程,希望对大家有所帮助

一、背景

之前在做录制回放平台的时候,需要前端展示子调用信息,子调用是一个请求列表数组结构,jsoneditor对数组的默认展示结构是[0].[1].[2]..的方式,为了达到如下的效果,必须用到 onNodeName的钩子函数,因此深入调研了下vue3如何集成jsoneditor

最后做出来的效果图

onNodeName的参考文档 github.com/josdejong/jsoneditor/blob/master/docs/api.md

二、参考方案

json-editor-vue3 感谢这位老哥的方案,我看了下源码,没有满足我的需要,核心就是属性需要自己加,因此我拿着他的代码改了下

三、代码实现

安装依赖 jsoneditor

npm install --save jsoneditor

jsoneditor是个开源的js的组件,参考文档 github.com/josdejong/jsoneditor

编写组件

目录结构如下

vue3-json-editor.tsx: 其中options的定义是完全参考jsoneditor的api文档的,具体需要什么功能,自己去实现对应的options即可!

import { ComponentPublicInstance, defineComponent, getCurrentInstance, onMounted, reactive, watch } from 'vue'
// @ts-ignore
// eslint-disable-next-line import/extensions
import JsonEditor from 'jsoneditor';
import 'jsoneditor/dist/jsoneditor.min.css';
// eslint-disable-next-line import/prefer-default-export
export const Vue3JsonEditor = defineComponent({
  props: {
    modelValue: [String, Boolean, Object, Array],
    showBtns: [Boolean],
    expandedOnStart: {
      type: Boolean,
      default: false
    },
    navigationBar: {
      type: Boolean,
      default: true
    },
    mode: {
      type: String,
      default: 'tree'
    },
    modes: {
      type: Array,
      default () {
        return ['tree', 'code', 'form', 'text', 'view']
      }
    },
    lang: {
      type: String,
      default: 'en'
    },
    onNodeName: {
      type: Function,
      default: ()=>{}
    }
  },
  setup (props: any, { emit }) {
    const root = getCurrentInstance()?.root.proxy as ComponentPublicInstance
    const state = reactive({
      editor: null as any,
      error: false,
      json: {},
      internalChange: false,
      expandedModes: ['tree', 'view', 'form'],
      uid: `jsoneditor-vue-${getCurrentInstance()?.uid}`
    })
    watch(
      () => props.modelValue as unknown as any,
      async (val) => {
        if (!state.internalChange) {
          state.json = val
          // eslint-disable-next-line no-use-before-define
          await setEditor(val)
          state.error = false
          // eslint-disable-next-line no-use-before-define
          expandAll()
        }
      }, { immediate: true })
    onMounted(() => {
      //这个options的定义是完全参考jsoneditor的api文档的
      const options = {
        mode: props.mode,
        modes: props.modes,
        onChange () {
          try {
            const json = state.editor.get()
            state.json = json
            state.error = false
            // eslint-disable-next-line vue/custom-event-name-casing
            emit('json-change', json)
            state.internalChange = true
            emit('input', json)
            root.$nextTick(function () {
              state.internalChange = false
            })
          } catch (e) {
            state.error = true
            // eslint-disable-next-line vue/custom-event-name-casing
            emit('has-error', e)
          }
        },
        onNodeName(v: object) {
          // eslint-disable-next-line vue/custom-event-name-casing
            return props.onNodeName(v);
        },
        onModeChange () {
          // eslint-disable-next-line no-use-before-define
          expandAll()
        },
        navigationBar: props.navigationBar
      }
      state.editor = new JsonEditor(
        document.querySelector(`#${state.uid}`),
        options,
        state.json
      )
      // eslint-disable-next-line vue/custom-event-name-casing
      emit('provide-editor', state.editor)
    })
    function expandAll () {
      if (props.expandedOnStart && state.expandedModes.includes(props.mode)) {
        (state.editor as any).expandAll()
      }
    }
    function onSave () {
      // eslint-disable-next-line vue/custom-event-name-casing
      emit('json-save', state.json)
    }
    function setEditor (value: any): void {
      if (state.editor) state.editor.set(value)
    }
    return () => {
      // @ts-ignore
      // @ts-ignore
      return (
        <div>
          <div id={state.uid} class={'jsoneditor-vue'}></div>
        </div>
      )
    }
  }
})

style.css

.ace_line_group {
  text-align: left;
}
.json-editor-container {
  display: flex;
  width: 100%;
}
.json-editor-container .tree-mode {
  width: 50%;
}
.json-editor-container .code-mode {
  flex-grow: 1;
}
.jsoneditor-btns {
  text-align: center;
  margin-top: 10px;
}
.jsoneditor-vue .jsoneditor-outer {
  min-height: 150px;
}
.jsoneditor-vue div.jsoneditor-tree {
  min-height: 350px;
}
.json-save-btn {
  background-color: #20a0ff;
  border: none;
  color: #fff;
  padding: 5px 10px;
  border-radius: 5px;
  cursor: pointer;
}
.json-save-btn:focus {
  outline: none;
}
.json-save-btn[disabled] {
  background-color: #1d8ce0;
  cursor: not-allowed;
}
code {
  background-color: #f5f5f5;
}

index.ts

export * from './vue3-json-editor';

四、如何使用

<template>
  <div class="container">
    <Vue3JsonEditor
        v-model="json"
        mode='view'
        :show-btns="true"
        :on-node-name="onNodeName"
    />
  </div>
</template>
<script lang="ts" setup>
import {computed,} from 'vue'
import {Vue3JsonEditor} from "@/components/json-editor";
const props = defineProps({
  record: {
    type: Object,
    default() {
      return {
        request: undefined,
      };
    },
  },
});
const json=computed(()=>{
  const {record} = props;
  return record.subInvocations;
});
// eslint-disable-next-line consistent-return
const onNodeName = (node: {
    value: any; type: any
})=>{
  if (node.type==='object' && node.value.identity) {
    return node.value.identity;
  }
  return undefined;
}
</script>
<script lang="ts">
export default {
  name: 'Invocations',
};
</script>
<style scoped lang="less">
.container {
  padding: 0 20px 20px 20px;
}
</style>

以上就是vue3集成jsoneditor的方法详解的详细内容,更多关于vue3集成jsoneditor的资料请关注脚本之家其它相关文章!

相关文章

  • Vue3 computed初始化获取设置值实现示例

    Vue3 computed初始化获取设置值实现示例

    这篇文章主要为大家介绍了Vue3 computed初始化以及获取值设置值实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Vue混入mixins分发组件可复用功能

    Vue混入mixins分发组件可复用功能

    混入 (mixin) 提供了一种非常灵活的方式,来分发 Vue 组件中的可复用功能。一个混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被“混合”进入该组件本身的选项
    2022-09-09
  • Vue 集成 PDF.js 实现 PDF 预览和添加水印的步骤

    Vue 集成 PDF.js 实现 PDF 预览和添加水印的步骤

    这篇文章主要介绍了如何在 Vue 中集成 Mozilla/PDF.js ,实现自定义的 PDF 预览器,以及给被预览的 PDF 添加水印
    2021-01-01
  • vue项目记录锁定和解锁功能实现

    vue项目记录锁定和解锁功能实现

    这篇文章主要为大家详细介绍了vue项目记录锁定和解锁功能实现,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Vue.js实现tab切换效果

    Vue.js实现tab切换效果

    这篇文章主要为大家详细介绍了Vue.js实现tab切换效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • vue使用axios实现文件上传进度的实时更新详解

    vue使用axios实现文件上传进度的实时更新详解

    最近在学习axios,然后项目就用到了,所以这篇文章主要给大家介绍了关于vue中利用axios实现文件上传进度的实时更新的相关资料,文中先对axios进行了简单的介绍,方法大家理解学习,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12
  • Vue的模板语法以及实战案例

    Vue的模板语法以及实战案例

    Vue使用了基于HTML的模板语法,允许开发者声明式地将DOM绑定至底层Vue实例的数据,下面这篇文章主要给大家介绍了关于Vue的模板语法以及案例的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • ant design vue嵌套表格及表格内部编辑的用法说明

    ant design vue嵌套表格及表格内部编辑的用法说明

    这篇文章主要介绍了ant design vue嵌套表格及表格内部编辑的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • 在Vue组件上动态添加和删除属性方法

    在Vue组件上动态添加和删除属性方法

    下面小编就为大家分享一篇在Vue组件上动态添加和删除属性方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-02-02
  • Vue过滤器使用方法详解

    Vue过滤器使用方法详解

    过滤器的功能是对要显示的数据进行格式化后再显示,其并没有改变原本的数据,只是产生新的对应的数据,下面这篇文章主要给大家介绍了关于Vue中过滤器定义以及使用方法的相关资料,需要的朋友可以参考下
    2022-12-12

最新评论