vue封装wangEditor

news/2024/7/24 10:39:20 标签: vue.js, javascript, 前端

components下面创建WangEditor.vue

<template>
  <div>
    <toolbar
      style="border-bottom: 1px solid #ccc"
      :editor="editor"
      :defaultConfig="toolbarConfig"
      :mode="mode"
    />
    <editor
      style="height: 500px; overflow-y: hidden"
      v-model="modifiedContent"
      :defaultConfig="defaultEditorConfig"
      :mode="mode"
      @onCreated="onCreated"
      @onChange="sendMessage"
    />
  </div>
</template>

<script>
import globalConfig from "@/config";
const { zixun_host } = globalConfig;
import Vue from "vue";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
const defaultEditorConfig = {
  // JS 语法
  MENU_CONF: {},
  // 其他属性...
};
// 修改 uploadImage 菜单配置
defaultEditorConfig.MENU_CONF["uploadImage"] = {
  server: "上传图片地址",
  fieldName: "file",
  maxFileSize: 5 * 1024 * 1024, // 5M
  timeout: 30 * 1000, // 30秒
  //【注意】不需要修改的不用写,wangEditor 会去 merge 当前其他配置
};
export default Vue.extend({
  name: "WangEditor",
  props: {
    content: String,
    toolbarConfig: Object,
  },
  data() {
    return {
      modifiedContent: "",
      headers: {
        Authorization:"token"
      },
      editor: null,
      defaultEditorConfig: defaultEditorConfig,
      mode: "default", // or 'simple'
    };
  },
  components: {
    Editor,
    Toolbar,
  },
  /**
   * 通过监听props的变化,在watch选项中进行处理。
   * @author  我
   */
  watch: {
    content(newVal) {
      this.modifiedContent = newVal;
    },
    modifiedContent(newVal) {
      // 在这里可以进行进一步处理
      //   console.log("Modified Content:", newVal);
    },
  },
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy(); // 组件销毁时,及时销毁编辑器
  },
  created() {},
  mounted() {},
  methods: {
    //编辑器创建完毕时的回调函数。
    onCreated(editor) {
      this.editor = Object.seal(editor); // 一定要用 Object.seal() ,否则会报错
      console.log(this.editor);
    },
    sendMessage() {
      this.$emit("message-sent", this.modifiedContent);
    },
  },
});
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>

main.js中注册这个全局组件
通过这种方式,可以在每个页面上使用组件,而无需在每个页面中重复导入和注册"@wangeditor/editor-for-vue"组件。记得确保已经安装好"@wangeditor/editor-for-vue"依赖。
组件之间可以通过props和事件来进行参数传递
使用 ↓

		<div style="border: 1px solid #ccc; width: 500px">
          <WangEditor
            :content="form.content"
            :toolbarConfig="toolbarConfig"
            @message-sent="handleMessage"
          />
        </div>
const toolbarConfig = {};
toolbarConfig.excludeKeys = [
  "todo", //待办
  "emotion", //表情
  "insertLink", //超链接
  "insertVideo", //表情
  "insertImage", //网络图片
  "group-video", //视频
  "codeBlock", //代码块
  "divider", //分割线
  "fullScreen", //全屏
];
data() {
    return {
      form: formInit(),
      toolbarConfig: toolbarConfig,
    };
  },
	/**
     * 接收组件传来的值
     * @author  我
     */
    handleMessage(message) {
      console.log("Received message:", message);
      this.form.content = message;
    },

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

相关文章

Navicat连接mysql8.0:提示无法加载身份验证插件“caching_sha2_password”

Navicat连接mysql时&#xff0c;提示&#xff1a;Unable to load authentication plugin ‘caching_sha2_password‘. 原因&#xff1a;mysql 8.0 默认使用 caching_sha2_password 身份验证机制。 D:\MySQL8.0\install\bin>mysql -uroot -p123456789 #登录 mysql: [War…

MYSQL学习之——管理用户

MYSQL学习之——管理用户&#xff08;DCL&#xff09; 用户这个东西其实是一个和TABLE DATABASE 这种东西一样的并列关键字。 用户的管理无外乎几个操作 查看用户 添加用户 删除用户 更新用户名或密码 改变用户对数据库的操作权限。 MYSQL语句功能备注USE mysql; select * FR…

js 根据键判断值

最原始的写法&#xff1a; 改进后的写法&#xff1a; const DeviceTypeObj {SO2: "SO<sub>2</sub>",CO: "CO",NO: "NO",NO2: "NO<sub>2</sub>",O3: "O<sub>3</sub>", let value Dev…

pip安装mysqlclient报错 Exception: Can not find valid pkg-config name

今天docker内搭建python3.10环境时报这个错误&#xff0c;安装 mysqlclient 时报错。 WARNING: The directory /home/seluser/.cache/pip or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions…

使用“vue init mpvue/mpvue-quickstart“初始化mpvue项目时出现的错误及解决办法

当使用"vue init mpvue/mpvue-quickstart"初始化 mpvue 项目时出现 "vue-cli Failed to download repo mpvue/mpvue-quickstart: connect ETIMEDOUT IP地址"原因是 github 的 IP 解析失败&#xff0c;连接超时 解决办法&#xff1a;更改最新的 github 的 …

【LeetCode】301.删除无效的括号

题目 给你一个由若干括号和字母组成的字符串 s &#xff0c;删除最小数量的无效括号&#xff0c;使得输入的字符串有效。 返回所有可能的结果。答案可以按 任意顺序 返回。 示例 1&#xff1a; 输入&#xff1a;s "()())()" 输出&#xff1a;["(())()"…

Solidity 小白教程:10. 控制流,用 solidity 实现插入排序

Solidity 小白教程&#xff1a;10. 控制流&#xff0c;用 solidity 实现插入排序 这一讲&#xff0c;我们将介绍solidity中的控制流&#xff0c;然后讲如何用solidity实现插入排序&#xff08;InsertionSort&#xff09;&#xff0c;一个看起来简单&#xff0c;但实际上很容易…

MySQL 中 MyISAM 与 InnoDB 引擎的区别

分析&回答 区别很多&#xff0c;大家说出下面几点&#xff0c;面试就应该 OK 了 1) 事务支持 MyISAM不支持事务&#xff0c;而InnoDB支持。InnoDB的AUTOCOMMIT默认是打开的&#xff0c;即每条SQL语句会默认被封装成一个事务&#xff0c;自动提交&#xff0c;这样会影响速…