网站首页 > 技术文章 正文
下载Vue-Quill-Editor
npm install vue-quill-editor --save
下载quill(Vue-Quill-Editor需要依赖)
npm install quill --save
代码
<template>
<div class="about">
<div class="editor_container">
<quill-editor
v-model="content"
ref="myQuillEditor"
:options="editorOption"
>
</quill-editor>
<input type="text" v-model="texts"/>
<button type="button" @click="onclickForm">提交</button>
</div>
</div>
</template>
<script>
import {quillEditor} from "vue-quill-editor" //调用编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
name: 'about',
components: {quillEditor},
data() {
return {
content: '',
texts: '',
// 编辑器配置
editorOption: {
placeholder: '在这里输入内容',
modules: {
toolbar: [
['bold', 'italic', 'underline', 'strike'], // 加粗、倾斜、下划线、删除线
['blockquote', 'code-block'],// 引用代码块
[{'header': 1}, {'header': 2}], // 标题,键值对应的形式,1,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'], //清除
['image', 'video'], // 上传图片、上传视频
]
}
}
}
},
mounted() {
let content = ''; // 请求返回值
this.str = this.escapeStringHTML(content)
},
methods: {
// 转码
escapeStringHTML(str) {
str = str.replace(/</g, '<');
str = str.replace(/>/g, '>');
return str
},
onclickForm() {
console.log(this.content)
this.content = ''
}
},
computed: {
editor() {
return this.$refs.myQuillEditor.quill
}
}
}
</script>
<style scoped>
.editor_container /deep/ .ql-editor {
min-height: 300px;
}
</style>
自定义 toolbar 菜单
// 编辑器配置
editorOption: {
placeholder: '在这里输入内容',
modules: {
toolbar: [
['bold', 'italic', 'underline', 'strike'], // 加粗、倾斜、下划线、删除线
['blockquote', 'code-block'],// 引用代码块
[{'header': 1}, {'header': 2}], // 标题,键值对应的形式,1,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'], //清除
['image', 'video'], // 上传图片、上传视频
]
}
}
存储及将数据库中的数据反显为HTML字符串
后台接收到数据后会将字符中的标签进行转码,所以我们要先进行一个解码的操作让他变成标签形式的字符串:
例如后台接收的数据如下:"<h1>title</h1>" ,对应解码后就是`<h1>title</h1>`。
// 把实体格式字符串转义成HTML格式的字符串
// 转码
escapeStringHTML(str) {
str = str.replace(/</g, '<');
str = str.replace(/>/g, '>');
return str
},
然后将返回值赋值给对应的参数:
<div v-html='str' class='ql-editor'>{{str}}</div>
上面的str就是转码函数返回的值,我们要先在data中定义,所以我现在将新增跟展示放在一起,代码如下:
<template>
<div class="about">
<div class="editor_container">
<quill-editor
v-model="content"
ref="myQuillEditor"
:options="editorOption"
>
</quill-editor>
<input type="text" v-model="texts"/>
<button type="button" @click="onclickForm">提交</button>
</div>
</div>
</template>
<script>
import {quillEditor} from "vue-quill-editor" //调用编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
name: 'about',
components: {quillEditor},
data() {
return {
content: '',
texts: '',
// 编辑器配置
editorOption: {
placeholder: '在这里输入内容',
modules: {
toolbar: [
['bold', 'italic', 'underline', 'strike'], // 加粗、倾斜、下划线、删除线
['blockquote', 'code-block'],// 引用代码块
[{'header': 1}, {'header': 2}], // 标题,键值对应的形式,1,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'], //清除
['image', 'video'], // 上传图片、上传视频
]
}
}
}
},
mounted() {
let content = ''; // 请求返回值
this.str = this.escapeStringHTML(content)
},
methods: {
// 转码
escapeStringHTML(str) {
str = str.replace(/</g, '<');
str = str.replace(/>/g, '>');
return str
},
onclickForm() {
console.log(this.content)
this.content = ''
}
},
computed: {
editor() {
return this.$refs.myQuillEditor.quill
}
}
}
</script>
<style scoped>
.editor_container /deep/ .ql-editor {
min-height: 300px;
}
</style>
猜你喜欢
- 2024-10-13 「干货」Deno TCP Echo Server 是怎么运行的?
- 2024-10-13 Vue.js的6个最佳表单生成器组件(vue 自定义表单组件)
- 2024-10-13 Github 上 36 个最实用的 Vue 开源库
- 2024-10-13 Vue3 插件开发详解尝鲜版「值得收藏」
- 2024-10-13 基于 Express 应用框架的技术方案选型浅谈
- 2024-10-13 超实用!基于前端vue.js生态开源项目
- 2024-10-13 细品Npm 依赖处理的进化史(npm安装依赖命令)
- 2024-10-13 Vue原来可以这样写开发效率杠杠的
- 2024-10-13 带你了解 vue-next(Vue 3.0)之 炉火纯青「实践」
- 2024-10-13 Webpack Vue瘦身,感受快到飞起的加载速度!
- 最近发表
- 标签列表
-
- cmd/c (64)
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- sqlset (64)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)