Vue + Element UI 使用富文本编辑器

本文详细介绍了如何在Vue项目中结合Element UI使用富文本编辑器,包括下载组件、引入、注册、放置及配置基本数据等六个步骤。
摘要由CSDN通过智能技术生成

第一步,先下载组件

npm install vue-quill-editor

第二步,在需要使用的位置引入(富文本组件)

import { quillEditor } from 'vue-quill-editor'

第三步,引入相关的css

import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

第四步,注册组件(因为这个是组件,所以要先注册一下)

components: {
        quillEditor
    }

第五步,放到需要的盒子中

    <quill-editor
      ref="text"
      v-model="content"
      class="myQuillEditor"
      :options="editorOption"
    />
    <el-button type="primary" @click="submit">提交</el-button>
    <div v-html="data"></div>

第六步,配置基本数据


                
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用VueElement UI来实现一个富文本编辑器,同时也可以使用第三方库Quill来实现。以下是使用Element UI和Quill实现的代码示例: 使用Element UI: 1. 首先需要安装Element UI和Quill: ``` npm install element-ui quill --save ``` 2. 在Vue组件中引入Element UI和Quill: ```javascript import { quillEditor } from 'vue-quill-editor' import 'quill/dist/quill.core.css' import 'quill/dist/quill.snow.css' import 'quill/dist/quill.bubble.css' import { Button, Input } from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' ``` 3. 在Vue组件中使用Element UI和Quill: ```html <template> <div> <el-button type="primary" @click="bold">加粗</el-button> <el-button type="primary" @click="red">标红</el-button> <quill-editor v-model="content" :options="editorOption"></quill-editor> </div> </template> <script> export default { components: { quillEditor, 'el-button': Button, 'el-input': Input }, data() { return { content: '', editorOption: { modules: { 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'] ] } } } }, methods: { bold() { const quill = this.$refs.myQuillEditor.quill const range = quill.getSelection() if (range) { quill.format('bold', true) } }, red() { const quill = this.$refs.myQuillEditor.quill const range = quill.getSelection() if (range) { quill.format('color', 'red') } } } } </script> ``` 使用Quill: 1. 首先需要安装Quill: ``` npm install quill --save ``` 2. 在Vue组件中引入Quill: ```javascript import Quill from 'quill' import 'quill/dist/quill.core.css' import 'quill/dist/quill.snow.css' import 'quill/dist/quill.bubble.css' ``` 3. 在Vue组件中使用Quill: ```html <template> <div> <button @click="bold">加粗</button> <button @click="red">标红</button> <div ref="editor"></div> </div> </template> <script> export default { data() { return { editor: null } }, mounted() { this.editor = new Quill(this.$refs.editor, { modules: { 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'] ] }, theme: 'snow' }) }, methods: { bold() { const range = this.editor.getSelection() if (range) { this.editor.format('bold', true) } }, red() { const range = this.editor.getSelection() if (range) { this.editor.format('color', 'red') } } } } </script> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值