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)

不过不影响运行构建

相关推荐
OpenTiny社区1 分钟前
TinyVue v3.31 更新速览:新组增件 + 文档优化,多项实用能力升级
vue.js
用户62960593247056 分钟前
一次位置调整引发的画布失忆:深入 Vue2 虚拟 DOM 复用机制
前端·vue.js
Sterting8 分钟前
条件渲染与列表渲染
前端·vue.js
木公子28 分钟前
Vue3源码精读03:响应式核心依赖追踪机制|track与trigger底层源码全解析
前端·vue.js
看到我请叫我铁锤1 小时前
vue编写web端在线预览文档
前端·javascript·vue.js
90后的晨仔13 小时前
Mac 开发 uni-app 终极方案:让安卓模拟器彻底脱离 Android Studio 独立运行
前端·vue.js
C++、Java和Python的菜鸟18 小时前
第12章 前端Web进阶(Vue工程化+ElementPlus)
前端·javascript·vue.js
妙码生花19 小时前
让各大 AI 整理出来的前端工程命名最简实践(Vue + TS)
前端·javascript·vue.js
用户8417948145621 小时前
vxe-table 实现编辑列的权限控制:列级与表级权限管理
vue.js
Tian_Hang21 小时前
Vue3 父子组件通讯
前端·javascript·vue.js