vue3引用vue-ueditor-wrap组件

参考文档:vue-ueditor-wrap - Vue + UEditor + v-model双向绑定Vue + UEditor + v-model双向绑定https://haochuan9421.github.io/vue-ueditor-wrap/#/home

1.安装组件

npm i vue-ueditor-wrap@3.0.8

2.下载 UEditor

下载链接:https://pan.baidu.com/s/1WEhVbXI_iHW8yU9O_z2hmQ?pwd=f9xo

放在public目录下

3.注册组件

main.ts

import VueUeditorWrap from 'vue-ueditor-wrap';
import { createApp } from "vue";
const app = createApp(App);
app.use(VueUeditorWrap)

4.封装组件

/src/components/ueditor/index.vue

<template>
    <vue-ueditor-wrap v-model="state.editorVal" :config="editorConfig" :editor-id="props.editorId" @ready="ueready"
        @input="handleInput" claas="ueditor-wrap"></vue-ueditor-wrap>
</template>
  
<script setup lang="ts" name="Ueditor">
import { ref, onMounted, reactive, watch, getCurrentInstance } from 'vue'

// 定义父组件传过来的值
const props = defineProps({
    editorId: {
        type: String,
        default: 'editor1'
    },
    // 双向绑定,用于获取 editor.getHtml()
    getHtml: String,
})

// 定义子组件向父组件传值/事件
const emit = defineEmits(['update:getHtml']);

const state = reactive({
    editorVal: props.getHtml,
});
const { proxy }: any = getCurrentInstance();
const editorConfig = {
    UEDITOR_HOME_URL: './UEditor/', // 访问 UEditor 静态资源的根路径,可参考常见问题1
}

function ueready(edt) {
    //WordPaster快捷键 Ctrl + V
    edt.addshortcutkey({
        "wordpaster": "ctrl+86"
    });
}
const handleInput = (val: string) => {
    // console.log("handleInput===", val);
    emit('update:getHtml', val);
};

watch(
    () => state.editorVal,
    (val) => {
        emit('update:getHtml', val);
    },
    {
        deep: true,
    }
);
</script>

<style scoped lang="scss"></style>

5.页面引用组件

<Ueditor editorId="Ueditor1" v-model:get-html="ruleForm.content"></Ueditor>
<Ueditor editorId="Ueditor2" v-model:get-html="ruleForm.wxcontent"></Ueditor>

import Ueditor from '/@/components/ueditor/index.vue';

components: {
	Ueditor
},

效果图

### 安装与配置 `vue-ueditor-wrap` 富文本编辑器 #### 安装依赖包 为了在 Vue 3 项目中集成 `vue-ueditor-wrap`,需通过 npm 或者 yarn 来安装特定版本的组件: ```bash npm install vue-ueditor-wrap@3.x --save ``` 或者使用 Yarn: ```bash yarn add vue-ueditor-wrap@3.x ``` 此命令会将适合 Vue 3 的 `vue-ueditor-wrap` 版本加入到项目的依赖列表之中[^1]。 #### 下载并准备 UEditor 文件夹 接着要下载 UEditor 并将其放置于公共目录下以便访问。这一步骤对于确保富文本编辑器能够正常加载资源文件至关重要[^2]。 #### 组件引入与注册 完成上述准备工作之后,在目标页面或全局范围内导入 `VueUeditorWrap` 组件,并按照如下方式定义其选项: ```javascript import { defineComponent } from &#39;vue&#39;; import VueUeditorWrap from &#39;vue-ueditor-wrap&#39;; // 引入组件 export default defineComponent({ name: "App", components: { VueUeditorWrap, // 注册组件 }, data() { return { content: &#39;<p>初始内容</p>&#39;, // 编辑区默认内容 config: { UEDITOR_HOME_URL: &#39;/static/UEditor/&#39;, // 设置静态资源路径 serverUrl: &#39;/api/upload&#39;, // 配置服务器接口地址 } }; } }); ``` 注意这里设置了两个重要属性:一个是用于指定 UEditor 资源位置的 `UEDITOR_HOME_URL`;另一个是指向上传图片等操作的服务端 URL 地址 `serverUrl`[^3]。 #### 使用示例 最后可以在模板部分像下面这样来调用已注册好的 `VueUeditorWrap` 组件: ```html <template> <div id="app"> <vue-ueditor-wrap v-model="content" :config="config"></vue-ueditor-wrap> </div> </template> ``` 以上即是在 Vue 3 应用程序里成功集成了 `vue-ueditor-wrap` 所必需执行的主要步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值