惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Troy Hunt's Blog
P
Proofpoint News Feed
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
Cyberwarzone
Cyberwarzone
T
Tor Project blog
Cisco Talos Blog
Cisco Talos Blog
S
Securelist
L
Lohrmann on Cybersecurity
Security Latest
Security Latest
T
Threatpost
H
Heimdal Security Blog
W
WeLiveSecurity
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY
IT之家
IT之家
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
TaoSecurity Blog
TaoSecurity Blog
A
About on SuperTechFans
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Azure Blog
Microsoft Azure Blog
Hugging Face - Blog
Hugging Face - Blog
Google DeepMind News
Google DeepMind News
量子位
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
B
Blog RSS Feed
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
AI
AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
Vercel News
Vercel News
C
Cyber Attacks, Cyber Crime and Cyber Security
Latest news
Latest news
D
Darknet – Hacking Tools, Hacker News & Cyber Security
大猫的无限游戏
大猫的无限游戏
Forbes - Security
Forbes - Security

博客园 - 前端架构师

Claude Code 源码架构分析 我不是狐狸,我是那Harness Engineering 小龙虾的自我养成之路 vue watch详解 微信授权没搞过? css3图片防止变形 公众号使用微信sdk的正确姿势 web离线应用前提之离线检测 es6手写一个call 同学给我来个手写的new吧 this综合篇 this绑定番外篇之箭头函数绑定 vue中引入公用过滤器? this详解下 012天this详解上 011天之跨域资源共享CORS 010天JSON.stringify()详解 009天之跨浏览器的事件处理程序 使用XHR上传文件要不要了解一下?
vue3+ts添加公共富文本组件
前端架构师 · 2021-07-19 · via 博客园 - 前端架构师

vue3+ts添加公共富文本组件

下载

npm install wangeditor --save-dev
复制代码

新建一个editor.vue 的组件

<style scoped>
.part_right {
    width: 100%;
    background: #f2f2f2;
    min-height: 100vh;
}
.list {
    width:96%;
    margin: 0 auto;

/* background: gray; */
padding-top: 50px;
}
.list ul li{
list-style-type: none;
display: flex;
border-bottom: 1px solid #e6e5e5;
min-height: 50px;
background: #d5d5d5;
}
.list ul li>div{
flex: 1;
line-height: 50px;
}
.list ol li{
list-style-type: none;
display: flex;
border-bottom: 1px solid #e6e5e5;
min-height: 30px;
}
.list ol li>div{
flex: 1;
line-height: 30px;
}
.flright {
float: right;
margin-right: 2%;
}
</style>
<template>
<div>
<div className="shop">
<div className="text-area" >
<div ref="editorElemMenu"
style="backgroundColor: '#f1f1f1'; border:'1px solid #ccc'"
className="editorElem-menu">
</div>
<div style="height: 300;border: '1px solid #ccc',borderTop: 'none'" ref="editorElemBody" className="editorElem-body">
</div>
</div>
</div>
</div>

</template>

<script lang="ts">

import { onMounted ,reactive,toRefs,defineComponent,ref} from 'vue'
import E from 'wangeditor'

interface DataProps {
editor: any;
editorContent: string
getContent:(ref?: any) => void
}

export default defineComponent({
name:'Editor',
components: {
},
props: {
options: Object,
value: String,
},
setup(props, { emit }) {
const editorElemMenu = ref();
const editorElemBody = ref();
const data: DataProps = reactive({
editorContent: '',
editor:{},
getContent:() => {
console.log("111",props)
data.editor.txt.html(props.value)
}
})
onMounted(() => {
let _this = this;
const elemMenu = editorElemMenu.value;
const elemBody = editorElemBody.value;
data.editor = new E(elemMenu, elemBody)
// 使用 onchange 函数监听内容的变化,并实时更新到 state 中
data.editor.config.onchange = (html: any) => {
console.log(data.editor.txt.html())
data.editorContent = data.editor.txt.html()
// 向外部返回一个处理过的值
emit('onEditor', data.editor.txt.html())
emit('update:value', data.editor.txt.html())
}
data.editor.config.customUploadImg = function (files: any, insert: any ) {
// files 是 input 中选中的文件列表insert 是获取图片 url 后,插入到编辑器的方法
// let file;
// if (files && files.length) {
// file = files[0];
// } else {
// return
// }
// 图片上传
console.log("files1",files)

const formData = new FormData();
formData.append('file', files[0]);
console.log("files",files,insert)
// formData.append('Banners',{id:editorinfo.id,naviChildId:editorinfo.naviChildId})
}

data.editor.config.menus = [
'head', // 标题
'bold', // 粗体
'fontSize', // 字号
'fontName', // 字体
'italic', // 斜体
'underline', // 下划线
'strikeThrough', // 删除线
'foreColor', // 文字颜色
'backColor', // 背景颜色
'link', // 插入链接
'list', // 列表
'justify', // 对齐方式
'quote', // 引用
'emoticon', // 表情
'image', // 插入图片
'table', // 表格
'video', // 插入视频
'code', // 插入代码
'undo', // 撤销
'redo' // 重复
]
data.editor.config.uploadImgShowBase64 = true
data.editor.create()

// data.getContent()
})
const refData = toRefs(data);
return {
...refData,
editorElemMenu,
editorElemBody
}
}
})
</script>

复制代码

调用的组件

<style scoped>

</style>

<template>
<div class="part_right">
<Editor ref="Editor" :value="content" @onEditor="onEditor"/>
</div>
</template>
<script lang="ts">

import { onMounted ,reactive,toRefs,defineComponent,ref} from 'vue'
import Editor from '@/components/tool/editor.vue'

interface DataProps {
content: string;
showBack:(ref?: any) => void
}

export default defineComponent({
name:'about',
components: {
Editor
},
setup() {
const Editor = ref();
const data: DataProps = reactive({
content:'1',
//获取富文本中的内容
onEditor:(value: string) => {
console.log("父组件",value)
},
// 富文本回显
showBack:() => {
data.content = '回显内容公众号qdleader'
console.log(" data.content", data.content)
setTimeout(() =>{
Editor.value.getContent()
})

}
})

onMounted(() => {
data.showBack()
})
const refData = toRefs(data);
return {
...refData,
Editor
}
}
})
</script>

复制代码

这样就完成可以在vue3加ts中使用 富文本啦

拓展。。

有的同学需要自定义上传图片的接口

```

data.editor.config.customUploadImg = function (files: any, insert: any ) {
        // files 是 input 中选中的文件列表insert 是获取图片 url 后,插入到编辑器的方法
        // let file;
        // if (files && files.length) {
        //     file = files[0];
        // } else {
        //     return
        // }
        // 图片上传
        console.log("files1",files)

const formData = new FormData();
formData.append('file', files[0]);
console.log("files",files,insert)
// formData.append('Banners',{id:editorinfo.id,naviChildId:editorinfo.naviChildId})

// 把请求你们服务器地址获得的图片链接,替换到下面src里面即可
data.editor.txt.append("<img src='https://rmxgh-1305579889.cos.ap-beijing.myqcloud.com/img/64bec1a86509170772c19fc5f88d84e1.jpg' />")

}
```

扫码加群或领资料

微信图片_20200331101947.png