Vue2-Editor 富文本编辑器使用教程与示例详解
前言
Vue2-Editor 是一个基于 Vue.js 2.x 的富文本编辑器组件,它封装了 Quill 编辑器的核心功能,为开发者提供了简单易用的富文本编辑解决方案。本文将详细介绍 Vue2-Editor 的各种使用场景和配置方法,帮助开发者快速掌握这一工具。
基础使用
最基本的 Vue2-Editor 使用方式非常简单,只需要导入组件并绑定数据即可:
<template>
<div id="app">
<vue-editor v-model="content"></vue-editor>
</div>
</template>
<script>
import { VueEditor } from "vue2-editor";
export default {
components: {
VueEditor
},
data() {
return {
content: "<h1>初始内容</h1>"
};
}
};
</script>
这种基础配置已经包含了常用的文本编辑功能,如加粗、斜体、标题、列表等。
自定义图片上传处理
在实际项目中,我们通常需要自定义图片上传逻辑。Vue2-Editor 提供了 useCustomImageHandler 属性和 imageAdded 事件来实现这一需求:
<template>
<div id="app">
<vue-editor
useCustomImageHandler
@imageAdded="handleImageAdded"
v-model="htmlForEditor"
/>
</div>
</template>
<script>
import { VueEditor } from "vue2-editor";
import axios from "axios";
export default {
components: {
VueEditor
},
data() {
return {
htmlForEditor: ""
};
},
methods: {
handleImageAdded(file, Editor, cursorLocation, resetUploader) {
const formData = new FormData();
formData.append("image", file);
axios.post("https://your-api-endpoint.com/upload", formData)
.then(response => {
Editor.insertEmbed(cursorLocation, "image", response.data.url);
resetUploader();
})
.catch(error => {
console.error("上传失败:", error);
resetUploader();
});
}
}
};
</script>
关键点说明:
useCustomImageHandler属性启用自定义图片处理imageAdded事件会在用户选择图片后触发- 事件参数包含文件对象、编辑器实例、光标位置和重置上传器的方法
- 上传成功后使用
insertEmbed方法插入图片到正确位置
页面加载后设置内容
有时我们需要在组件挂载后异步获取编辑器内容:
<script>
export default {
// ...
mounted() {
setTimeout(() => {
this.content = "<p>这是异步加载的内容</p>";
}, 2000);
}
};
</script>
多编辑器实例
在同一个页面中使用多个编辑器实例也很简单,只需为每个实例设置不同的 v-model 绑定:
<template>
<div>
<vue-editor v-model="editor1Content"></vue-editor>
<vue-editor v-model="editor2Content"></vue-editor>
</div>
</template>
<script>
export default {
data() {
return {
editor1Content: "<h1>编辑器1的初始内容</h1>",
editor2Content: "<h1>编辑器2的初始内容</h1>"
};
}
};
</script>
<style>
.vue-editor {
height: 350px;
margin-bottom: 20px;
}
</style>
自定义工具栏
Vue2-Editor 允许自定义工具栏按钮和布局:
<template>
<vue-editor v-model="content" :editorToolbar="customToolbar"/>
</template>
<script>
export default {
data() {
return {
content: "",
customToolbar: [
["bold", "italic", "underline"],
[{ list: "ordered" }, { list: "bullet" }],
["image", "code-block"]
]
};
}
};
</script>
实时预览功能
实现编辑器内容的实时预览:
<template>
<div>
<vue-editor v-model="content"/>
<div class="preview" v-html="content"></div>
</div>
</template>
<style>
.preview {
border: 1px solid #ddd;
padding: 1rem;
margin-top: 1rem;
}
</style>
使用自定义 Quill 模块
Vue2-Editor 支持扩展 Quill 的功能模块,以下是两种注册方式:
方式一:手动注册(推荐)
<script>
import { VueEditor, Quill } from "vue2-editor";
import ImageResize from "quill-image-resize-module";
Quill.register("modules/imageResize", ImageResize);
export default {
data() {
return {
content: "",
editorSettings: {
modules: {
imageResize: {
// 模块配置
}
}
}
};
}
};
</script>
方式二:自动注册
<script>
import { VueEditor } from "vue2-editor";
import ImageResize from "quill-image-resize-module";
export default {
data() {
return {
content: "",
customModulesForEditor: [
{ alias: "imageResize", module: ImageResize }
],
editorSettings: {
modules: {
imageResize: {}
}
}
};
}
};
</script>
编程式控制编辑器焦点
通过 ref 可以获取编辑器实例并控制焦点:
<template>
<div>
<button @click="focusEditor">聚焦编辑器</button>
<vue-editor ref="myEditor" v-model="content"/>
</div>
</template>
<script>
export default {
methods: {
focusEditor() {
this.$refs.myEditor.quill.focus();
}
}
};
</script>
事件监听
Vue2-Editor 提供了多种事件供开发者监听:
<template>
<vue-editor
v-model="content"
@text-change="onTextChange"
@selection-change="onSelectionChange"
@focus="onEditorFocus"
@blur="onEditorBlur"
@ready="onEditorReady"
/>
</template>
<script>
export default {
methods: {
onTextChange(delta, oldDelta, source) {
console.log("内容变化:", delta);
},
onSelectionChange(range, oldRange, source) {
console.log("选择范围变化:", range);
},
onEditorFocus(editor) {
console.log("编辑器获得焦点");
},
onEditorBlur(editor) {
console.log("编辑器失去焦点");
},
onEditorReady(editor) {
console.log("编辑器准备就绪");
}
}
};
</script>
结语
Vue2-Editor 提供了丰富的功能和灵活的配置选项,能够满足大多数富文本编辑需求。通过本文介绍的各种使用场景和技巧,开发者可以快速构建功能完善的文本编辑功能。在实际项目中,建议根据具体需求选择合适的配置方式,并注意处理图片上传等异步操作。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



