Nuxt3 整合 wangeditor

网上找了很多,基本是无法使用的,所以在自己整合了后记录一下。
1.加入plugins
在plugins目录下创建一个wang-editor.ts

import { Editor, Toolbar } from '@wangeditor/editor-for-vue'

export default defineNuxtPlugin((nuxt) => {
    nuxt.vueApp.component('WeEditor', Editor)
    nuxt.vueApp.component('WeToolbar', Toolbar)
})

2.在根目录下的nuxt.config.ts中关闭上述plugin文件的ssr。注意,我们在以前可能会使用ssr:false,但在最新版本下已经被弃用了,如果使用的话可能会报错Element is not found,可以参考官方文档:https://nuxt.com.cn/docs/api/configuration/nuxt-config#plugins-1

  plugins: [
    {
      src: '~/plugins/wang-editor',
      mode: 'client',
    }
  ],

3.在components下创建wangeditor组件,名字自己取

<template>
  <div style="border: 1px solid #ccc; margin-top: 10px">
    <WeToolbar style="border-bottom: 1px solid #ccc" :editor="editorRef" :defaultConfig="toolbarConfig" :mode="mode" />

    <WeEditor style="height: 300px; overflow-y: hidden" v-model="valueHtml" :defaultConfig="editorConfig" :mode="mode"
      @onCreated="handleCreated" @onChange="handleChange" @onDestroyed="handleDestroyed" @onFocus="handleFocus"
      @onBlur="handleBlur" @customAlert="customAlert" @customPaste="customPaste" />
  </div>
</template>


<script  setup lang="ts">
import '@wangeditor/editor/dist/css/style.css' // 引入 css
// import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue'

const mode = 'default'

const isClient = ref(false)
if (process.client) {
  isClient.value = true
}

// 编辑器实例,必须用 shallowRef
const editorRef = shallowRef()

// 内容 HTML
const valueHtml = ref('<p></p>')

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

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

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


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

const handleChange = (editor: any) => {
  console.log('change:', editor.getHtml());
};

const handleDestroyed = (editor: any) => {
  console.log('destroyed', editor);
};

const handleFocus = (editor: any) => {
  console.log('focus', editor);
};

const handleBlur = (editor: any) => {
  console.log('blur', editor)
}
const customAlert = (info: any, type: any) => { alert(`【自定义提示】${type} - ${info}`) }
const customPaste = (editor: any, event: any, callback: any) => {
  console.log('ClipboardEvent 粘贴事件对象', event)
  // const html = event.clipboardData.getData('text/html') // 获取粘贴的 html
  // const text = event.clipboardData.getData('text/plain') // 获取粘贴的纯文本
  // const rtf = event.clipboardData.getData('text/rtf') // 获取 rtf 数据(如从 word wsp 复制粘贴)

  // 自定义插入内容
  editor.insertText('xxx')

  // 返回 false ,阻止默认粘贴行为
  event.preventDefault()
  callback(false) // 返回值(注意,vue 事件的返回值,不能用 return)

  // 返回 true ,继续默认的粘贴行为
  // callback(true)
}
</script> 



<style scoped></style>

最终效果:
在这里插入图片描述

Nuxt.js 结合 Wangeditor 是一种结合富文本编辑器(WangEditor)和 Vue.js 框架在 Nuxt 网站项目中的应用,它提供了一个易用的界面让用户插入、编辑和管理文章内容。当涉及到附件上传时,通常会涉及到前端文件处理和后端API交互。 在 NuxtWangeditor 中,你可以按照以下步骤配置附件上传功能: 1. **安装插件**:首先,你需要安装一个支持图片上传的 Wangeditor 插件,如 `wangeditor-uploader` 或者直接集成官方支持的图片上传组件。 ```bash npm install wangEditor-uploader ``` 2. **配置编辑器**:在你的 Nuxt 组件中引入并初始化编辑器,并添加上传功能。例如: ```html <template> <div> <w-editor :config="editorConfig" @upload-success="handleUploadSuccess"></w-editor> </div> </template> <script> import WEditor from 'wangEditor' import Uploader from 'wangEditor-uploader' export default { components: { WEditor, Uploader }, data() { return { editorConfig: { // ...其他配置 uploadImgUrl: '/api/upload-image', // 图片上传API地址 uploadFileName: (file) => file.name, // 文件名处理函数 allowedTypes: ['image/jpeg', 'image/png'] // 允许上传的文件类型 } } }, methods: { handleUploadSuccess(file, response) { // 存储图片路径或者更新到数据库 this.$set(this.editorContent, 'imgList', [{ src: response.data.url }]); } } } </script> ``` 3. **后端接口**:创建一个 API 接口用于接收上传的文件,保存到服务器指定目录,并返回新的图片URL给前端。 注意,这只是一个基本示例,实际项目中可能需要处理更多的边缘情况,比如错误处理、并发控制等。此外,还要确保服务器的安全措施,例如限制文件大小、检查文件类型等。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值