nuxt3接入wangEditor

基础环境

shell 复制代码
"nuxt": "^3.11.2",
"vue": "^3.4.27",
node: v20.12.2

安装

shell 复制代码
pnpm add @wangeditor/editor -S
pnpm add @wangeditor/editor-for-vue -S

注意看看包的版本是不是都是 5.x

bash 复制代码
"@wangeditor/editor": "^5.1.23",
"@wangeditor/editor-for-vue": "^5.1.12"

截止到 2024-05-22 上面2个包均为最新版本

代码

html 复制代码
<template>
  <div>
    <div style="border: 1px solid #ccc">
      <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>
  </div>
</template>

<script setup>
import '@wangeditor/editor/dist/css/style.css'; // 引入 css
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';

// 编辑器实例,必须用 shallowRef
const editorRef = shallowRef();
// 内容 HTML
const valueHtml = ref('<p>hello</p>');

const toolbarConfig = {};
const editorConfig = { placeholder: '请输入内容...' };

const mode=ref('default')

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

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例,重要!
};

onMounted(() => {
  // 模拟 ajax 异步获取内容
  setTimeout(() => {
    valueHtml.value = '<p>模拟 Ajax 异步设置内容</p>';
  }, 1500);
});
</script>

上述组件目前不需要包裹在 Nuxt3ClientOnly 里面。

经过测试,pnpm run build 构建 , pnpm run preview 预览 都没问题

在终端运行dev 的时候,会出现

less 复制代码
 WARN  [Vue Router warn]: uncaught error during route navigation:


 ERROR  Cannot set property crypto of #<Object> which has only a getter

  at node_modules/.pnpm/@wangeditor+editor@5.1.23/node_modules/@wangeditor/editor/dist/index.js:69:25
  at node_modules/.pnpm/@wangeditor+editor@5.1.23/node_modules/@wangeditor/editor/dist/index.js:2:66
  at Object.<anonymous> (node_modules/.pnpm/@wangeditor+editor@5.1.23/node_modules/@wangeditor/editor/dist/index.js:5:3)
  at Module._compile (node:internal/modules/cjs/loader:1369:14)
  at Object..js (node:internal/modules/cjs/loader:1427:10)
  at Module.load (node:internal/modules/cjs/loader:1206:32)
  at Function._load (node:internal/modules/cjs/loader:1022:12)
  at cjsLoader (node:internal/modules/esm/translators:366:17)
  at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:315:7)
  at ModuleJob.run (node:internal/modules/esm/module_job:222:25)

不过不影响运行构建

相关推荐
奋斗吧程序媛3 小时前
补充一个小知识点:有关@click.native
前端·vue.js
英勇无比的消炎药3 小时前
一行命令背后:TinyRobot CLI 如何重构 AI 对话接入的效率范式
vue.js·aigc
jay神5 小时前
基于 FastAPI + Vue 的宠物领养管理系统
前端·vue.js·python·毕业设计·fastapi·宠物
一杯奶茶¥5 小时前
水果销售网站 CRM客户信息管理系统 超市管理系 酒店管理系统 健身房管理系统 在线音乐网站 校园招聘系统
java·vue.js·spring boot·mysql·spring·java项目
英勇无比的消炎药6 小时前
一站式搞定品牌风格:TinyRobot 主题定制从入门到精通
vue.js
尽欢i8 小时前
Vue3 customRef 封神教程:防抖、本地存储、自动埋点一套搞定,模板干干净净
前端·javascript·vue.js
因_崔斯汀8 小时前
Vue 模板编译:HTML 是怎么变成 JS 的?
前端·vue.js
英勇无比的消炎药9 小时前
样式随心定制:TinyRobot 样式覆写与 CSS 变量实战解析
vue.js
疯狂的魔鬼9 小时前
多角色督办任务详情页:从权限矩阵到组件拆分的完整实现
前端·vue.js·架构
英勇无比的消炎药9 小时前
拆解内核:深入分析 TinyRobot 输入区组件设计与实现原理
vue.js