从0到1:通用后台管理系统 Vue3使用wangEditor

news/2024/7/10 0:46:35 标签: 前端, vue3, 前端小王hs, vue, javascript

那么这一节我们在编辑公司信息的弹窗中使用富文本插件wangEditor官网

Vue3使用wangEditor

  • 安装wangEditor
  • 在弹窗中引入wangEditor结构
  • api接口部分
  • editor组件script部分
  • 怎么去修改富文本的编辑器?

案例内效果:
富文本

安装wangEditor

npm install @wangeditor/editor --save
npm install @wangeditor/editor-for-vue@next --save

安装完成

在弹窗中引入wangEditor结构

文档部分:
在这里插入图片描述

代码部分:

<template>
	<el-dialog v-model="state.dialogFormVisible" :title="title" width="600px" destroy-on-close>
		<div style="border: 1px solid #ccc">
		<!-- wangEditor结构 --> 
			<Toolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig"
				:mode="mode" />
			<Editor style="height: 500px; overflow-y: hidden;" v-model="valueHtml" :defaultConfig="editorConfig"
				:mode="mode" @onCreated="handleCreated" />
		</div>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="cancel">取消</el-button>
				<el-button type="primary" @click="yes">
					确认
				</el-button>
			</span>
		</template>
	</el-dialog>
</template>

api接口部分

后端
需要注意的是,在35集《完成轮播图以及公司介绍接口》中
我们的参数是set_value,类型是varchar,但文案我们应该使用text类型
所以我们新建一个字段set_text,用来存放我们的公司介绍等信息
同时,我们新增了一个接口,通过mysql like语法获取所有的信息

javascript">// 编辑公司介绍的接口 参数 set_text set_name
exports.changeCompanyIntroduce = (req, res) => {
	const sql = 'update setting set set_text = ? where set_name = ? '
	db.query(sql, [req.body.set_text, req.body.set_name], (err, result) => {
		if (err) return res.cc(err)
		res.send({
			status: 0,
			message: '修改公司介绍成功'
		})
	})
}

// 获取公司介绍 参数 set_name
exports.getCompanyIntroduce = (req, res) => {
	const sql = 'select * from setting where set_name = ?'
	db.query(sql, req.body.set_name,(err, result) => {
		if (err) return res.cc(err)
		res.send(result[0].set_value)
	})
}

// 新增接口 获取所有的公司介绍 用于首页
exports.getAllCompanyIntroduce = (req, res) => {
	// res.send(req.body.set_name)
	const sql = "select * from setting where set_name like '公司%' "
	db.query(sql, (err, result) => {
		if (err) return res.cc(err)
		res.send(result)
	})
}

前端\

// 修改公司介绍
export const changeCompanyIntroduce= (set_text,set_name) => {
	return instance({
		url: '/set/changeCompanyIntroduce',
		method: 'POST',
		data:{
			set_text,
			set_name
		}
	})
}

// 获取公司介绍/组织结构/公司战略/高层信息
export const getCompanyIntroduce= set_name => {
	return instance({
		url: '/set/getCompanyIntroduce',
		method: 'POST',
		data:{
			set_name
		}
	})
}

// 获取所有介绍信息
export const getAllCompanyIntroduce = () => {
	return instance({
		url: '/set/getAllCompanyIntroduce ',
		method: 'POST',
	})
}

editor组件script部分

<script lang="ts" setup>
	import '@wangeditor/editor/dist/css/style.css' // 引入 css
	import {
		onBeforeUnmount, ref, shallowRef,
		reactive
	} from 'vue'
	import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
	import { bus } from "@/utils/mitt.js"
	import {
		ElMessage
	} from "element-plus"
	// changecompanyintro
	import { changeCompanyIntroduce, getCompanyIntroduce} from '@/api/set'
	const title = ref()
	bus.on("editorTitle", async (id : number) => {
		if (id == 1) {
			title.value = '编辑公司介绍'
			valueHtml.value = await getCompanyIntroduce('公司介绍')
		}
		if (id == 2) {
			title.value = '编辑公司架构'
			valueHtml.value = await getCompanyIntroduce('公司架构')
		}
		if (id == 3) {
			title.value = '编辑公司战略'
			valueHtml.value = await getCompanyIntroduce('公司战略')
		}
		if (id == 4) {
			title.value = '编辑高层介绍'
			valueHtml.value = await getCompanyIntroduce('高层介绍')
		}
	})
	// 编辑器实例,必须用 shallowRef
	const editorRef = shallowRef()
	// mode
	const mode = ref('default')
	// 内容 HTML
	const valueHtml = ref()
	const toolbarConfig = {
		excludeKeys: []
	}
	const editorConfig = {
		placeholder: '请输入内容...',
		MENU_CONF: {
			uploadImage: {
				//上传图片配置
				server: 'http://127.0.0.1:3007/set/uploadCompanyPicture', //上传接口地址
				fieldName: 'file', //上传文件名
				methods: 'post',
				metaWithUrl: true, // 参数拼接到 url 上
				// 单个文件上传成功之后
				// onSuccess(file, res) {
				// },
				// 自定义插入图片
				customInsert(res, insertFn) {
					insertFn(res.url)
				},
			},
		}
	}
	// 上传图片,修改 uploadImage 菜单配置
	// 需要注意的是,如何去修改参数?
	toolbarConfig.excludeKeys = [
		'blockquote',
		'bgColor',
		'color',
		'group-more-style',
		'fontFamily',
		'bulletedList',
		'numberedList',
		'lineHeight',
		'todo',
		'emotion',
		'insertLink',
		'group-video',
		'insertTable',
		'codeBlock',
		'divider',
		'fullScreen',
		// 'group-image',

		// 排除菜单组,写菜单组 key 的值即可
	]
	// 点击确认 修改文案
	const yes = async () => {
		// 去除 编辑两个字
		console.log(title.value.slice(-4))
		// 两个参数 set_text set_name
		const res = await changeCompanyIntroduce(valueHtml.value,title.value.slice(-4))
		console.log(res)
		if (res.status == 0) {
			ElMessage({
				message: '修改公司介绍成功!',
				type: 'success',
			})
			state.dialogFormVisible = false
		} else {
			state.dialogFormVisible = false
			ElMessage.error('修改公司介绍失败,请检查网络是否通畅!')
		}
	}


	// 组件销毁时,也及时销毁编辑器
	onBeforeUnmount(() => {
		const editor = editorRef.value
		if (editor == null) return
		editor.destroy()
	})

	const handleCreated = (editor : any) => {
		editorRef.value = editor // 记录 editor 实例,重要!
	}
	const state = reactive({
		dialogFormVisible: false,
	});

	// 取消删除
	const cancel = () => {
		ElMessage.error("取消赋权!");
		state.dialogFormVisible = false;
	};

	// 暴露open方法
	const open = () => {
		state.dialogFormVisible = true;
	};
	defineExpose({
		open,
	});

	// 取消订阅/监听
	onBeforeUnmount(() => {
		bus.all.clear()
	})

怎么去修改富文本的编辑器?

①找到对应的data-menu-key
menu-key
②放入toolbarConfig.excludeKeys
在这里插入图片描述


http://www.niftyadmin.cn/n/4943342.html

相关文章

Keepalived入门指南:实现故障转移和负载均衡

文章目录 一、简介1. Keepalived概述2. 高可用性和负载均衡的重要性 二、故障转移1. 什么是故障转移2. Keepalived的故障转移原理a) VRRP协议b) 虚拟路由器ID和优先级 3. 配置Keepalived实现故障转移a) 主备服务器的设置b) 监控网络接口c) 虚拟IP的配置d) 备份服务器接管流程 三…

黑客入侵:福特汽车Sync3车机存在漏洞,黑客入侵可抹除系统数据

据福特汽车公告&#xff0c;他们发现部分2021年至2022年车型的Sync3车机存在Wi-Fi漏洞&#xff0c;该漏洞可能被黑客利用来入侵并抹除车机内的系统数据。这一漏洞源于福特车系中采用的WL18xx MCP驱动程序的内存缓冲区溢位漏洞&#xff0c;其漏洞编号为CVE-2023-29468。 这一发现…

Matplotlib绘图知识小结--Python数据分析学习

一、Pyplot子库绘制2D图表 1、Matplotlib Pyplot Pyplot 是 Matplotlib 的子库&#xff0c;提供了和 MATLAB 类似的绘图 API。 Pyplot 是常用的绘图模块&#xff0c;能很方便让用户绘制 2D 图表。 Pyplot 包含一系列绘图函数的相关函数&#xff0c;每个函数会对当前的图像进行…

Linux下TA_Lib安装失败的问题处理

Linux下TA_Lib安装失败的问题处理 TA_Lib是python的量化指标库&#xff0c;其中包含了很多150多种量化指标 &#xff0c;量化分析中经常使用。 This is a Python wrapper for TA-LIB based on Cython instead of SWIG. From the homepage: TA-Lib is widely used by trading …

Datawhale Django后端开发入门 TASK02 Admin管理员、外键的使用

1.Admin管理员的使用 先放一张成功的截图&#xff0c;记得自己创建时的账号和密码呀&#xff0c;如果忘了的话可以也是再重新创建管理员账号和密码的 &#xff0c;这个页面接下来就不用操作了,就要开始重要的 post 步骤。 二、外键的使用 我认为比较难的&#xff08;很不好操作…

C#,特性,属性,索引器

目录 特性 规定特性 预定义特性 1&#xff0c;AttributeUsage 2&#xff0c;Conditional 3&#xff0c;Obsolete 创建自定义特性 1&#xff0c;声明自定义特性 2&#xff0c;构建自定义特性 应用自定义特性 属性 访问器 抽象属性 索引器 语法 索引器的用途 重…

C语言实现哈希搜索算法

一、哈希搜索算法原理 哈希搜索&#xff0c;也叫散列查找&#xff0c;是一种通过哈希表&#xff08;散列表&#xff09;实现快速查找目标元素的算法。哈希搜索算法通常适用于需要快速查找一组数据中是否存在某个元素的场景&#xff0c;其时间复杂度最高为 O(1)&#xff0c;而平…

【Linux操作系统】举例解释Linux系统编程中文件io常用的函数

在Linux系统编程中&#xff0c;文件IO操作是非常常见和重要的操作之一。通过文件IO操作&#xff0c;我们可以打开、读取、写入和关闭文件&#xff0c;对文件进行定位、复制、删除和重命名等操作。本篇博客将介绍一些常用的文件IO操作函数。 文章目录 1. open()1.1 原型、参数及…