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)

不过不影响运行构建

相关推荐
加减法原则7 小时前
Vue3 组合式函数:让你的代码复用如丝般顺滑
前端·vue.js
天若有情6738 小时前
React、Vue、Angular的性能优化与源码解析概述
vue.js·react.js·angular.js
草巾冒小子9 小时前
vue3实战:.ts文件中的interface定义与抛出、其他文件的调用方式
前端·javascript·vue.js
eggcode9 小时前
Vue+Openlayers加载OSM、加载天地图
vue.js·openlayers·webgis
武昌库里写JAVA12 小时前
vue3面试题(个人笔记)
java·vue.js·spring boot·学习·课程设计
lalalalalalalala13 小时前
开箱即用的 Vue3 无限平滑滚动组件
前端·vue.js
前端Hardy13 小时前
8个你必须掌握的「Vue」实用技巧
前端·javascript·vue.js
久爱@勿忘14 小时前
第二章:创建登录页面
前端·vue.js·elementplus
Jinxiansen021114 小时前
Vue 3 中父子组件双向绑定的 4 种方式
javascript·vue.js·ecmascript
木依14 小时前
Vue3 Element plus table有fixed列时错行
javascript·vue.js·elementui