当前位置: 首页 > wzjs >正文

济南经三路专业做网站成都网站建设 3e网络

济南经三路专业做网站,成都网站建设 3e网络,wordpress首页调用文章多张图片,合肥seo收费在后台管理系统或 CMS 中,富文本编辑器是必不可少的一环。为了更好地控制交互细节、接口集成和样式定制,我们将基于 Vue 3 TypeScript 封装一个可复用的富文本组件 —— JEditor,并使用 WangEditor 官方提供的 Vue 适配包。 安装依赖 yarn …

在后台管理系统或 CMS 中,富文本编辑器是必不可少的一环。为了更好地控制交互细节、接口集成和样式定制,我们将基于 Vue 3 + TypeScript 封装一个可复用的富文本组件 —— JEditor,并使用 WangEditor 官方提供的 Vue 适配包。


安装依赖

yarn add @wangeditor/editor-for-vue@next
# 或者 npm install @wangeditor/editor-for-vue@next --save

你也可以参考官方文档了解更多配置及插件信息:https://www.wangeditor.com/


完整组件源码

<script setup lang="ts">
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import '@wangeditor/editor/dist/css/style.css'
import { IDomEditor, IEditorConfig, IToolbarConfig } from '@wangeditor/editor'
import { toolbarConfigDefault } from './defaultConfig'
import { ref, watch, shallowRef, onBeforeUnmount } from 'vue'defineOptions({name: 'JEditor',inheritAttrs: false
})type InsertFnType = (url: string, alt: string, href: string) => voidconst props = defineProps({editorId: {type: String,default: 'j-editor'},modelValue: {type: String,required: true},height: {type: [String, Number],default: '400px'},readonly: {type: Boolean,default: false},toolbarConfig: { type: Object as () => IToolbarConfig, default: () => ({}) }, // 工具栏配置editorConfig: { type: Object as () => IEditorConfig, default: () => ({}) } // 编辑器配置
})const emit = defineEmits(['update:modelValue', 'change'])// 编辑器实例,必须用 shallowRef
const editorRef = shallowRef<IDomEditor | null>(null)const valueHtml = ref('')// 编辑器配置
const mergedConfig = {placeholder: '请输入内容...',readOnly: props.readonly,MENU_CONF: {// 上传图片配置uploadImage: {server: 'https://xxxxxxxx', //服务端地址maxFileSize: 10 * 1024 * 1024, // 10M// 自定义上传参数。参数会被添加到 formData 中,一起上传到服务端。meta: {},// 自定义增加 http  headerheaders: {Accept: '*',authorization: 'Bearer xxxxxxxx'},// 超时时间timeout: 5 * 1000,// form-data fieldName,后端接口参数名称,默认值wangeditor-uploaded-imagefieldName: 'file',// 上传之前触发onBeforeUpload(file: File) {return file},// 自定义插入图片customInsert(res: any, insertFn: InsertFnType) {let d = res.data// url(图片 src ,必须) alt(图片描述文字,非必须) href(图片的链接,非必须)insertFn(d.fileUrl, d.fileName, d.fileUrl)}},// 上传视频配置uploadVideo: {}},...props.editorConfig
}const toolbarConfig = computed(() => Object.assign({}, toolbarConfigDefault, props.toolbarConfig))const editorStyle = computed(() => {return {height: isNumber(props.height) ? `${props.height}px` : props.height}
})// 初始化编辑器实例
function initEditor(editorInstance: IDomEditor) {editorRef.value = editorInstance
}// 监听modelValue变化
watch(() => props.modelValue,(newVal: string) => {if (newVal !== valueHtml.value) {valueHtml.value = newVal}},{immediate: true}
)watch(() => valueHtml.value,(val: string) => {emit('update:modelValue', val)}
)
// 编辑器内容变化回调
const handleChange = (editor) => {emit('change', editor)
}// 销毁编辑器
onBeforeUnmount(() => {const editor = editorRef.valueif (editor == null) returneditor.destroy()
})const isNumber = (val: any) => {return typeof val === 'number' && !isNaN(val)
}
</script><template><div class="jMdEditor"><Toolbar:editorId="editorId":editor="editorRef":defaultConfig="toolbarConfig"v-bind="$attrs"mode="default"/><Editor:editorId="editorId"v-model="valueHtml":defaultConfig="mergedConfig"mode="default"@onCreated="initEditor"@onChange="handleChange":style="editorStyle"/></div>
</template><style scoped lang="scss">
.jMdEditor {border: 1px solid #ccc;border-radius: 4px;
}
</style>
export const toolbarConfigDefault = {toolbarKeys: ['headerSelect', // 标题选择'bold', // 加粗'italic', // 斜体'through', // 删除线'underline', // 下划线'justifyCenter', // 居中对齐'justifyJustify', // 两端对齐'justifyLeft', // 左对齐'justifyRight', // 右对齐'bulletedList', // 无序列表'numberedList', // 有序列表'color', // 文字颜色'insertLink', // 插入链接'fontSize', // 字体大小'lineHeight', // 行高'delIndent', // 缩进'indent', // 增进'divider', // 分割线'insertTable', // 插入表格'undo', // 撤销'redo', // 重做'clearStyle', // 清除格式'fullScreen', // 全屏'blockquote', // 引用'codeBlock', // 代码块'insertImage', // 插入图片'uploadImage', // 上传图片'insertVideo' // 插入视频]
}

核心亮点解析

  1. 双向绑定与回显
    使用 v-model + watch 保证外部 modelValue 与内部 valueHtml 同步,组件内容更改后自动向父组件派发更新事件。

  2. 编辑器实例管理
    通过 shallowRef 存储 editorRef,并在 onBeforeUnmount 中销毁实例,避免内存泄漏。

  3. 高度可配置

    • props.editorConfig / props.toolbarConfig 支持用户自定义全部配置。

    • 图片、视频上传功能已集成,示例展示了自定义 customInsert 的典型用法。

  4. 样式灵活
    height 属性既可接收数字(自动追加 px),也可传入字符串(如 %rem 等),满足多种布局需求。


富文本编辑器的可扩展性非常强,你可以在此基础上进一步添加:

  • Markdown 支持

  • 公式与图表插入

  • 代码高亮插件

希望这篇文章能帮助你快速搭建一个可复用的富文本组件,让你的项目编辑体验更加专业、高效!


文章转载自:

http://09onvhcN.gLbnc.cn
http://yVxh4qeD.gLbnc.cn
http://bpHC2bIz.gLbnc.cn
http://QHzoSLPK.gLbnc.cn
http://6s3Ij8xW.gLbnc.cn
http://DYc2CQsP.gLbnc.cn
http://oAdaRRMR.gLbnc.cn
http://qeN8RVGq.gLbnc.cn
http://etXgldrw.gLbnc.cn
http://UHldnetT.gLbnc.cn
http://EF1q0FyN.gLbnc.cn
http://ygRS45r9.gLbnc.cn
http://TretfIVO.gLbnc.cn
http://6xzBSi5J.gLbnc.cn
http://NJyGCGHp.gLbnc.cn
http://jQ1POMSu.gLbnc.cn
http://N79jyYTz.gLbnc.cn
http://lTNMI5eA.gLbnc.cn
http://RqTQfriL.gLbnc.cn
http://ZcCHVjKS.gLbnc.cn
http://4M82FoX4.gLbnc.cn
http://15SSaGRr.gLbnc.cn
http://7VKJETSv.gLbnc.cn
http://U43JTvxy.gLbnc.cn
http://55HANeti.gLbnc.cn
http://FrLD46AM.gLbnc.cn
http://MZXY6510.gLbnc.cn
http://268Jo1C9.gLbnc.cn
http://m8FUlAdJ.gLbnc.cn
http://SR59PujU.gLbnc.cn
http://www.dtcms.com/wzjs/640820.html

相关文章:

  • 枣阳建网站怎么注册公司logo
  • 怎么封闭网站建站能赚钱吗
  • 怎样建设自己的视频网站首页北京网页设计公司兴田德润优选
  • 资阳网站seo易优cms插件
  • 影视网站建设目的泰安高端网站建设报价
  • 网站建设简历自我评价律师如何做网络推广
  • php装修网站源码网站开发框架具体是什么
  • 网站建设系统哪家好wordpress 编辑权限设置
  • html网站留言板代码发卡网站建设7az
  • 杭州做网站哪家好网页设计全部代码
  • 糗百网站源码学做电商那个网站好
  • 手机号交易网站源码seo长沙
  • 梅州建站多少钱wordpress 返回顶部代码
  • 哪些网站做二手挖机大理建设学校官方网站
  • 企业网站代码专业做淘宝网站绍兴
  • 制作网站建设入门青少年活动中心网站建设依据
  • 做个网站商场需要多少wap网站定位
  • 手机 网站制作婴儿辅食中企动力提供网站建设
  • ajax做网站企业建设高端网站的目的
  • 极速网站建设定制多少钱北京软件公司有多少家
  • 阜阳网站制作公司去哪找头像代做网站
  • 深圳福田专业网站改版设计一个网站策划书
  • 深圳福田网站建设专业公司住房和城乡建设部网站北京
  • 百度的官方网站网站美工主要工作是什么
  • 免费注册qq号网站erp企业管理系统平台
  • 网站建设怎么购买空间建设银行网站怎么修改手机号码吗
  • 怎么做传奇网站图做下载网站用什么程序好
  • 免费网站建立郑州汉狮做网站好不
  • wordpress怎么更换站点石材公司网站
  • 美团网站建设规划书提高网站公信力 单仁