Vue:Bin Code Editor格式化JSON編輯器詳解
最終效果如下圖所示:
Bin Code Editor安裝
npm或yarn安裝命令如下:
npm i bin-code-editor -S # or yarn add bin-code-editor
組件注冊
全局注冊
在 main.js 中寫入導入以下內容,
import Vue from 'vue'; import CodeEditor from 'bin-code-editor'; import 'bin-code-editor/lib/style/index.css'; Vue.use(CodeEditor);
局部注冊
在需要使用Bin Code Editor的組件中導入以下內容,
import { CodeEditor } from 'bin-code-editor' import 'bin-code-editor/lib/style/index.css'; export default { components: { CodeEditor }, }
使用方式
基本使用
value:必需,對應要轉換的 JavaScript 值(通常為對象或數(shù)組),也可使用v-model代替,值的類型為String字符串,對于JavaScript對象,可以使用JSON.stringfy函數(shù)進行轉換。
<template> <div> <b-code-editor v-model="jsonStr" :indent-unit="4" height="auto"/> </div> </template> <script> const jsonData = `{"title":"測試json數(shù)據(jù)","children":[{"name":"子項名稱", "desc":"子項說明" },{"name":"子項名稱1", "desc":"子項說明1" }]}` export default { data() { return { jsonStr: jsonData } } } </script>
組件屬性
組件事件與方法
踩坑指南
坑點描述
修改v-model綁定值之后,需要點擊一下編輯區(qū)才能顯示。
坑點解決:nextTick
將修改綁定值的操作,放在nextTick內部執(zhí)行,例如:
this.$nextTick(()=>{ //TODO:假設綁定值為query this.query = JSON.stringify({...});//轉為字符串 });
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue基礎之事件簡寫、事件對象、冒泡、默認行為、鍵盤事件實例分析
這篇文章主要介紹了vue基礎之事件簡寫、事件對象、冒泡、默認行為、鍵盤事件,結合實例形式分析了vue.js事件簡寫、冒泡及阻止冒泡等相關操作技巧,需要的朋友可以參考下2019-03-03Vue3中defineEmits、defineProps?不用引入便直接用
這篇文章主要介紹了Vue3中defineEmits、defineProps?不用引入便直接用,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09vue使用keep-alive實現(xiàn)組件切換時保存原組件數(shù)據(jù)方法
這篇文章主要介紹了vue使用keep-alive實現(xiàn)組件切換時保存原組件數(shù)據(jù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10