Vue3中使用富文本編輯器的示例詳解
在vue3中我們可以使用@wangeditor/editor、@wangeditor/editor-for-vue來實(shí)現(xiàn)其功能
npm地址:https://www.npmjs.com/package/@wangeditor/editor-for-vue/v/5.1.12?activeTab=readme
官網(wǎng):Editor
1. 安裝
pnpm add @wangeditor/editor # 或者 npm install @wangeditor/editor --save pnpm add @wangeditor/editor-for-vue@next # 或者 npm install @wangeditor/editor-for-vue@next --save
2. 組件封裝
@/comps/Editor/index.vue
首先為了能讓vue認(rèn)識(shí)@wangeditor/editor-for-vue庫(kù)、我們可以在.d.ts中聲明一下即可
// 聲明外部 npm 插件模塊 declare module '@wangeditor/editor-for-vue';
<template> <div class="Wft-Editor flex flex-col"> <Toolbar class="default-border" :editor="editorRef" :mode="mode" /> <Editor class="flex-1 overflow-y-auto" v-model="valueHtml" :defaultConfig="editorConfig" :mode="mode" @onCreated="handleCreated" @onChange="onChange" /> </div> </template> <script setup lang="ts"> import '@wangeditor/editor/dist/css/style.css' import { Editor, Toolbar } from '@wangeditor/editor-for-vue' import type { IDomEditor } from '@wangeditor/editor' import { onBeforeUnmount, shallowRef, computed, watch } from 'vue' type TProps = { mode?: string, valueHtml?: string, placeholder?: string, disable?: boolean } const props = withDefaults(defineProps<TProps>(), { mode: 'default', // or 'simple' valueHtml: '', placeholder: '請(qǐng)輸入內(nèi)容...', disable: false }) type TEmits = { (e: 'update:valueHtml', params: string): void (e: 'update:valueText', params: string): void (e: 'onChange', params: IDomEditor): void } const emits = defineEmits<TEmits>() const editorRef = shallowRef() const valueHtml = computed({ get() { return props.valueHtml }, set(valueHtml) { emits('update:valueHtml', valueHtml) } }) watch(() => props.disable, bool => { if(!editorRef.value) return bool ? (editorRef.value as IDomEditor).disable() : (editorRef.value as IDomEditor).enable() }, { deep: true }) const editorConfig = computed(() => { return { placeholder: props.placeholder } }) // 記錄 editor 實(shí)例,重要! const handleCreated = (editor: IDomEditor) => { editorRef.value = editor } // editor 改變 const onChange = (editor: IDomEditor) => { emits('onChange', editor) } // 銷毀編輯器 onBeforeUnmount(() => { if(!editorRef.value) return editorRef.value.destroy() }) function getHtml() { return (editorRef.value as IDomEditor).getHtml() } function getText() { return (editorRef.value as IDomEditor).getText() } defineExpose({ getHtml, getText }) </script> <style scoped> .Wft-Editor { width: 100%; height: 100%; } .flex { display: flex; } .flex-col { flex-direction: column; } .default-border { border-bottom: 1px solid #ccc; } .flex-1 { flex: 1; } .overflow-y-auto { overflow-x: hidden; overflow-y: auto; } </style>
3. 使用
<template> <div class="wel"> <div class="btn"> <button @click="submit">提交</button> <button @click="getHtml">獲取HTML</button> <button @click="getText">獲取TEXT</button> <button @click="editorDisable = !editorDisable">{{ editorDisable ? '啟用' : '禁用' }}</button> </div> <div class="editor-container"> <Editor ref="EditorRef" :disable="editorDisable" v-model:value-html="editorValue" @on-change="editorChange" /> </div> </div> </template> <script setup lang="ts"> import Editor from '@/comps/Editor/index.vue' import { onMounted, ref, shallowRef } from 'vue' import type { IDomEditor } from '@wangeditor/editor' let editorValue = ref('') // 提交的數(shù)據(jù) let editorDisable = ref(false) let EditorRef = shallowRef<InstanceType<typeof Editor>>() onMounted(() => { setTimeout(() => { editorValue.value = '<h1>回顯測(cè)試</h1>' }, 3000) }) const submit = () => { console.log(editorValue.value) } const getHtml = () => { console.log(EditorRef.value?.getHtml()) } const getText = () => { console.log(EditorRef.value?.getText()) } const editorChange = (editor: IDomEditor) => { console.log(editor.getHtml()) console.log(editor.getText()) } </script> <style scoped> .wel { width: 100%; height: 100%; } .btn { width: 100%; height: 40px; display: flex; align-items: center; } .btn button { margin-left: 15px; } .editor-container { width: 100%; height: calc(100% - 40px); } </style>
4. 效果
到此這篇關(guān)于Vue3中使用富文本編輯器的示例詳解的文章就介紹到這了,更多相關(guān)Vue3富文本編輯器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中根據(jù)時(shí)間戳判斷對(duì)應(yīng)的時(shí)間(今天 昨天 前天)
這篇文章主要介紹了vue中 根據(jù)時(shí)間戳 判斷對(duì)應(yīng)的時(shí)間(今天 昨天 前天),需要的朋友可以參考下2019-12-12vue實(shí)現(xiàn)ToDoList簡(jiǎn)單實(shí)例
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)ToDoList簡(jiǎn)單實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02vue2利用html2canvas+jspdf動(dòng)態(tài)生成多頁(yè)P(yáng)DF方式
利用vue2結(jié)合html2canvas和jspdf,可以實(shí)現(xiàn)前端頁(yè)面內(nèi)容導(dǎo)出為PDF的功能,首先需要安裝相關(guān)依賴,使用html2canvas將指定div內(nèi)容捕捉為圖像,再通過jspdf將圖像轉(zhuǎn)換為PDF2024-09-09基于vue組件實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了基于vue組件實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11vue實(shí)現(xiàn)頁(yè)面切換滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)頁(yè)面切換滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06Vue中常用rules校驗(yàn)規(guī)則(實(shí)例代碼)
這篇文章主要介紹了Vue中常用rules校驗(yàn)規(guī)則,本文通過實(shí)例代碼個(gè)大家介紹了一些校驗(yàn)方法,需要的朋友可以參考下2019-11-11Vue.JS實(shí)現(xiàn)垂直方向展開、收縮不定高度模塊的JS組件
這篇文章主要介紹了Vue.JS實(shí)現(xiàn)垂直方向展開、收縮不定高度模塊的JS組件,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06