b-code-editor通过$nextTick方法清除数据

文章讲述了作者在使用Vue的jsonEditor组件时遇到数据清除问题,发现可能是因为DOM节点创建延迟导致的。通过使用Vue的$nextTick方法确保在DOM更新后执行清除操作,解决了这个问题。

点击修改按钮json数据正常显示出来

但是点击新增我发现数据没有被清空

我在这做清除操作竟然没有作用

我又做了一个按钮触发

this.$refs['jsonEditor'].getEditor().setValue('')

我发现有用了

奇怪了,我怀疑是dom节点没有创建,导致清除不成功

所有我使用$nextTick方法

这个回调函数会在下一次DOM更新循环结束后被执行。
在上面的代码中,我将设置编辑器值的逻辑放在了this.$nextTick的回调函数中。这样可以确保在下一次DOM更新循环结束后再尝试设置编辑器的值,从而避免在组件mounted时就立即设置值导致的问题。

this.$nextTick(() => {
  this.$refs['jsonEditor'].getEditor().setValue('')
})
要配置 `quill-editor` 的自定义工具栏,可以通过设置 `modules` 属性来实现。具体来说,可以定义 `toolbar` 模块,并为其指定一个包含按钮组的数组。每个按钮组可以是一个数组,包含多个按钮名称或自定义按钮配置。以下是一个详细的配置示例: ```javascript const editorModules = { toolbar: [ ['bold', 'italic', 'underline', 'strike'], // 基本字体样式 ['blockquote', 'code-block'], // 引用和代码块 [{ 'header': 1 }, { 'header': 2 }], // 标题 [{ 'list': 'ordered' }, { 'list': 'bullet' }], // 列表样式 [{ 'script': 'sub' }, { 'script': 'super' }], // 上标和下标 [{ 'indent': '-1' }, { 'indent': '+1' }], // 缩进调整 [{ 'direction': 'rtl' }], // 文字方向 [{ 'size': ['small', false, 'large', 'huge'] }], // 字号 [{ 'header': [1, 2, 3, 4, 5, 6, false] }], // 多级标题 [{ 'color': [] }, { 'background': [] }], // 颜色选择 [{ 'font': [] }], // 字体选择 [{ 'align': [] }], // 对齐方式 ['clean'], // 清除格式 ['link', 'image', 'video'] // 插入链接、图片、视频 ] }; ``` 在 Vue 中使用 `vue-quill-editor` 时,可以通过 `modules` 属性绑定上述配置对象,如下所示: ```vue <template> <quill-editor v-model="content" :modules="editorModules" /> </template> <script> import { quillEditor } from 'vue-quill-editor'; export default { components: { quillEditor }, data() { return { content: '', editorModules: { toolbar: [ ['bold', 'italic', 'underline', 'strike'], ['blockquote', 'code-block'], [{ 'header': 1 }, { 'header': 2 }], [{ 'list': 'ordered' }, { 'list': 'bullet' }], [{ 'script': 'sub' }, { 'script': 'super' }], [{ 'indent': '-1' }, { 'indent': '+1' }], [{ 'direction': 'rtl' }], [{ 'size': ['small', false, 'large', 'huge'] }], [{ 'header': [1, 2, 3, 4, 5, 6, false] }], [{ 'color': [] }, { 'background': [] }], [{ 'font': [] }], [{ 'align': [] }], ['clean'], ['link', 'image', 'video'] ] } }; } }; </script> ``` 通过上述配置,可以灵活地控制 `quill-editor` 工具栏中显示的按钮和功能,满足不同场景下的需求。 ### 添加按钮提示 如果需要为工具栏中的按钮添加提示(如 `title` 属性),可以通过自定义工具栏按钮的渲染方式实现。例如,可以在 `mounted` 钩子中遍历工具栏的按钮元素,并为其添加 `title` 属性[^1]: ```javascript mounted() { this.$nextTick(() => { const toolbarButtons = document.querySelectorAll('.ql-toolbar button'); toolbarButtons.forEach(button => { const buttonClass = Array.from(button.classList).find(cls => cls.startsWith('ql-')); if (buttonClass) { const buttonName = buttonClass.replace('ql-', ''); button.title = buttonName; // 可根据需要自定义提示内容 } }); }); } ``` 通过这种方式,可以为每个按钮添加清晰的提示信息,提升用户体验。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值