Vue3中jsoneditor的使用示例詳解
更新時(shí)間:2024年01月11日 16:42:24 作者:Mrceel
這篇文章主要為大家詳細(xì)介紹了Vue3中jsoneditor的使用相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解下
vue3 使用 jsoneditor
復(fù)制代碼放到文件中就能用了
jsoneditor.vue
<template> <div ref="jsonDom" style="width: 100%; height: 460px"></div> </template> <script setup lang="ts"> import { ref, onMounted, watchEffect } from 'vue' import JsonEditor from 'jsoneditor' interface validateResult { path: Array<string | number> message: string } const props = defineProps<{ option: any validate?: (val: any) => validateResult }>() const emit = defineEmits(['update:modelValue', 'change', 'customValidation']) const jsonDom = ref(null) const validate = (res: any, editor: any) => { try { emit('change', { success: res.length === 0 && typeof editor.get() !== 'number', json: editor.getText() }) } catch (error) { console.log(error) } } onMounted(() => { const options = { history: false, sortObjectKeys: false, mode: 'code', modes: ['code', 'text'], onChange() { editor.validate().then((res: any) => validate(res, editor)) }, onBlur() { try { editor.set(eval(`(${editor.getText()})`)) editor.validate().then((res: any) => validate(res, editor)) } catch (error) { console.log(error) } }, onValidate: props.validate, onValidationError: function (errors: any) { errors.forEach((error: any) => { switch (error.type) { case 'validation': // schema validation error break case 'customValidation': // custom validation error emit('customValidation') break case 'error': // json parse error emit('change', { success: false, json: editor.getText() }) break } }) } } const editor = new JsonEditor(jsonDom.value, options) watchEffect(() => { editor.set(props.option) editor.validate().then((res: any) => validate(res, editor)) }) }) </script>
到此這篇關(guān)于Vue3中jsoneditor的使用示例詳解的文章就介紹到這了,更多相關(guān)Vue3 jsoneditor內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
el-select如何獲取下拉框選中l(wèi)abel和value的值
在開發(fā)業(yè)務(wù)場(chǎng)景中我們通常遇到一些奇怪的需求,例如el-select業(yè)務(wù)場(chǎng)景需要同時(shí)獲取我們選中的label跟 value,下面這篇文章主要給大家介紹了關(guān)于el-select如何獲取下拉框選中l(wèi)abel和value的值,需要的朋友可以參考下2022-10-10在Vue3中創(chuàng)建和使用自定義指令的實(shí)現(xiàn)方式
在現(xiàn)代前端開發(fā)中,Vue.js 是一個(gè)非常流行的框架,它提供了許多強(qiáng)大的功能來幫助開發(fā)者構(gòu)建高效的用戶界面,自定義指令是 Vue.js 的一個(gè)重要特性,它允許開發(fā)者擴(kuò)展 HTML 元素的功能,本文將詳細(xì)介紹如何在 Vue3 中創(chuàng)建和使用自定義指令,并提供示例代碼來幫助理解2024-12-12前端Vue學(xué)習(xí)之購(gòu)物車項(xiàng)目實(shí)戰(zhàn)記錄
購(gòu)物車是電商必備的功能,可以讓用戶一次性購(gòu)買多個(gè)商品,下面這篇文章主要給大家介紹了關(guān)于前端Vue學(xué)習(xí)之購(gòu)物車項(xiàng)目的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07Vue3時(shí)間軸組件問題記錄(時(shí)間信息收集組件)
本文介紹了如何在Vue3項(xiàng)目中封裝一個(gè)時(shí)間信息收集組件,采用雙向綁定響應(yīng)式數(shù)據(jù),通過對(duì)Element-Plus的Slider組件二次封裝,實(shí)現(xiàn)時(shí)間軸功能,解決了小數(shù)計(jì)算導(dǎo)致匹配問題和v-model綁定組件無效問題,感興趣的朋友跟隨小編一起看看吧2024-09-09