Vue3 JSON Editor
vue3-json-editor is a Vue.js 3 component that provides an interactive JSON editor. It's a direct fork of the `vue-json-editor` library, specifically tailored for Vue 3 applications, distinguishing it from older Vue 2 compatible versions. The package, currently at version 1.1.5, allows developers to display and edit JSON objects within their Vue components, supporting various modes like 'tree', 'view', and 'form'. It offers features such as displaying save buttons, expanding the editor on start, and handling different languages. The component provides `v-model` binding for the JSON data and emits events for changes (`json-change`), saves (`json-save`), and errors (`has-error`). It supports modern module systems including ESM and CommonJS, making it compatible with various build setups like Vite and webpack. While no explicit release cadence is stated, updates appear to be driven by maintenance and new feature integration from its upstream source.
Common errors
-
Failed to resolve component: Vue3JsonEditor
cause The Vue3JsonEditor component was not properly registered or imported in the parent Vue component.fixEnsure you have `import { Vue3JsonEditor } from 'vue3-json-editor'` in your script and added it to the `components` option: `components: { Vue3JsonEditor }`. -
TypeError: Cannot read properties of undefined (reading 'mode')
cause The `v-model` bound JSON data was not initialized as an object or was otherwise `undefined` or `null` when the component tried to access its properties.fixInitialize your `v-model` data property with an empty object (e.g., `{}`) or a valid JSON object. For example, `const state = reactive({ json: {} })`. -
Uncaught TypeError: can't access property 'exports', ks is undefined (or similar module resolution errors)
cause This typically indicates a module resolution incompatibility, often occurring in Vite/ESM-first environments trying to consume a library with CommonJS peculiarities, or a breaking change in how the package bundles its exports.fixFor Vite projects, if the issue persists, consider adding `@originjs/vite-plugin-commonjs` to your `vite.config.js` to improve CommonJS compatibility. Also, review recent changes in the library's changelog for breaking build-related changes.
Warnings
- breaking This library is a fork specifically for Vue 3. It is not compatible with Vue 2 projects, and attempting to use it in a Vue 2 environment will result in errors related to Vue 3-specific APIs and component registration.
- gotcha The `v-model` binding for `vue3-json-editor` expects a JSON object for 'tree' mode and a JSON string for 'text' mode. Mismatching the data type with the editor's `mode` property can lead to unexpected behavior or errors.
- gotcha When using the `setup` function in Vue 3, reactive state for `v-model` must be correctly defined using `reactive` or `ref`. Directly assigning a plain object to `v-model` without reactivity will prevent proper updates from the editor.
- gotcha Some users have reported `TypeError: Cannot read property 'getMode' of null` or similar runtime errors, especially in specific build environments like Quasar with Vue 3 CLI or after certain version updates, suggesting issues with module resolution or component initialization.
Install
-
npm install vue3-json-editor -
yarn add vue3-json-editor -
pnpm add vue3-json-editor
Imports
- Vue3JsonEditor
const Vue3JsonEditor = require('vue3-json-editor')import { Vue3JsonEditor } from 'vue3-json-editor' - defineComponent
import { defineComponent } from 'vue' - reactive
import { reactive } from 'vue' - toRefs
import { toRefs } from 'vue'
Quickstart
<template>
<div>
<p>JSON Editor Example</p>
<Vue3JsonEditor
v-model="json"
:show-btns="true"
:expandedOnStart="true"
mode="tree"
@json-change="onJsonChange"
@has-error="onJsonError"
/>
</div>
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs } from 'vue'
import { Vue3JsonEditor } from 'vue3-json-editor'
export default defineComponent({
components: {
Vue3JsonEditor
},
setup () {
const state = reactive({
json: {
name: "John Doe",
age: 30,
isStudent: false,
courses: ["Math", "Science"]
}
})
function onJsonChange (value: object) {
console.log('JSON has changed:', value)
// You might want to update the reactive 'json' state here if not using v-model directly.
}
function onJsonError (error: any) {
console.error('JSON error:', error)
}
return {
...toRefs(state),
onJsonChange,
onJsonError
}
}
})
</script>
<style>
/* Basic styling for demonstration */
body { font-family: sans-serif; margin: 20px; }
div { max-width: 800px; margin: 0 auto; }
</style>