vue實(shí)現(xiàn)聊天框發(fā)送表情
vue聊天框發(fā)送表情以及vue界面發(fā)送表情實(shí)現(xiàn)的具體代碼,供大家參考,具體內(nèi)容如下
1.在發(fā)送消息的時(shí)候,判斷發(fā)送的消息是不是表情,表情的type:3,content:[愛(ài)心],這樣存儲(chǔ)在數(shù)據(jù)庫(kù)中
2.在獲取聊天記錄的時(shí)候,判斷type===3,處理表情,
<img v-else-if="chatItem.type === 3" :src="emojiUrl + emojiMap[chatItem.content]" style="width:25px;height:25px" />
1.textElement.vue
<template>
<div class="text-element-wrapper" >
<div class="text-element">
<div :class="isMine ? 'element-send' : 'element-received'">
<p>{{ date }}</p>
<!-- 文字 -->
<span>{{ chatItem.content }}</span>
<span v-if="chatItem.type === 1">{{ chatItem.content }}</span>
<!-- 表情 -->
<img v-else-if="chatItem.type === 3" :src="emojiUrl + emojiMap[chatItem.content]" style="width:25px;height:25px" />
</div>
<div :class="isMine ? 'send-img' : 'received-img'">
<img :src="chatItem.from_headimg" width="40px" height="40px"/>
</div>
</div>
</div>
</template>
<script>
// import decodeText from '../../../untils/decodeText'
import { getFullDate } from '../../../untils/common'
import {emojiMap, emojiUrl} from '../../../untils/emojiMap'
export default {
name: 'TextElement',
props: ['chatItem', 'isMine'],
data() {
return {
emojiMap: emojiMap,
emojiUrl: emojiUrl,
}
},
computed: {
// contentList() {
// return decodeText(this.chatItem)
// },
// 時(shí)間戳處理
date () {
return getFullDate(new Date(this.chatItem.time * 1000))
},
}
}
</script>
<style scoped>
.text-element-wrapper {
position: relative;
max-width: 360px;
border-radius: 3px;
word-break: break-word;
border: 1px solid rgb(235, 235, 235);
}
.text-element {
padding: 6px;
}
.element-received {
max-width: 280px;
background-color: #fff;
float: right;
}
.received-img {
float: left;
padding-right: 6px;
}
.element-send {
max-width: 280px;
background: rgb(5, 185, 240);
float: left;
}
.send-img {
float: right;
}
</style>
vue界面發(fā)送表情實(shí)現(xiàn),主要是思路:
<template>
<section class="dialogue-section clearfix" >
<div class="row clearfix" v-for="(item,index) in msgs" :key = index>
<img :src="item.uid == myInfo.uid ? myInfo.avatar :otherInfo.avatar" :class="item.uid == myInfo.uid ? 'headerleft' : 'headerright'">
<p :class="item.uid == myInfo.uid ? 'textleft' : 'textright'" v-html="customEmoji(item.content)"></p>
</div>
</section>
<div id="emoji-list" class="flex-column" v-if="emojiShow"> //首先根據(jù)這個(gè)來(lái)判斷發(fā)送表情彈窗用不用出現(xiàn)
<div class="flex-cell flex-row" v-for="list in imgs">
<div class="flex-cell flex-row cell" v-for="item in list" @click="inputEmoji(item)">
<img :src="'./emoji/'+ item + '.png'">
</div>
</div>
</div>
</template>
<script>
import { sendMsg } from "@/ws"; //是一個(gè)長(zhǎng)連接
import _ from "lodash";//這個(gè)是js一個(gè)很強(qiáng)大的庫(kù)
import eventBus from '@/eventBus'//這是一個(gè)子父?jìng)鬟f的公共文件
console.log(emoji)
export default {
data() {
this.imgs = _.chunk(emoji, 6) //這個(gè)是調(diào)用lodash庫(kù)的chunk方法 把 六個(gè)元素分成一個(gè)數(shù)組只不過(guò)是emoji這個(gè)數(shù)組中的二維數(shù)組
return {
emojiShow: false //剛開始默認(rèn)不顯示 點(diǎn)擊按鈕顯示 點(diǎn)擊的按鈕上可以寫@click='emojiShow=emojiShow'這種寫法
};
},
methods: {
customEmoji(text) { //這個(gè)函數(shù)就是服務(wù)器端把傳過(guò)來(lái)的名稱轉(zhuǎn)化為圖片的
return text.replace(/\[([A-Za-z0-9_]+)\]/g, '<img src="./emoji/$1.png" style="width:30px; height:30px;">')
},
inputEmoji(pic) {
this.content += `[${pic}]`//傳過(guò)來(lái)的名字轉(zhuǎn)為圖片
}
};
</script>
<style scoped>
@import '../../assets/css/dialogue.css';
#emoji-list {
height: 230px;
background: #fff;
}
#emoji-list .cell {
line-height: 13vh;
border-right: 1rpx solid #ddd;
border-bottom: 1rpx solid #ddd;
}
.flex-row {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.flex-column {
display: flex;
flex-direction: column;
justify-content: center;
align-items: stretch;
}
.flex-cell {
flex: 1;
}
.cell img {
width: 35px;
height: 35px;
}
</style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中實(shí)現(xiàn)父子組件雙向數(shù)據(jù)流的三種方案分享
通常情況下,父子組件的通信都是單向的,或父組件使用props向子組件傳遞數(shù)據(jù),或子組件使用emit函數(shù)向父組件傳遞數(shù)據(jù),本文將嘗試講解Vue中常用的幾種雙向數(shù)據(jù)流的使用,需要的朋友可以參考下2023-08-08
vue3.0自定義指令(drectives)知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整體了一篇關(guān)于vue3.0自定義指令(drectives)知識(shí)點(diǎn)總結(jié),有興趣的朋友們可以學(xué)習(xí)下。2020-12-12
vue中axios的post請(qǐng)求,415錯(cuò)誤的問(wèn)題
這篇文章主要介紹了vue中axios的post請(qǐng)求,415錯(cuò)誤的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Vue發(fā)送Formdata數(shù)據(jù)及NodeJS接收方式
這篇文章主要介紹了Vue發(fā)送Formdata數(shù)據(jù)及NodeJS接收方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
vue中實(shí)現(xiàn)子組件相互切換且數(shù)據(jù)不丟失的策略詳解
項(xiàng)目為數(shù)據(jù)報(bào)表,但是一個(gè)父頁(yè)面中有很多的子頁(yè)面,而且子頁(yè)面中不是相互關(guān)聯(lián),但是數(shù)據(jù)又有聯(lián)系,所以本文給大家介紹了vue中如何實(shí)現(xiàn)子組件相互切換,而且數(shù)據(jù)不會(huì)丟失,并有詳細(xì)的代碼供大家參考,需要的朋友可以參考下2024-03-03
HBuilder導(dǎo)入vue項(xiàng)目并通過(guò)域名訪問(wèn)的過(guò)程詳解
這篇文章主要介紹了HBuilder導(dǎo)入vue項(xiàng)目并通過(guò)域名訪問(wèn),一般情況下運(yùn)行vue項(xiàng)目需要安裝node.js,通過(guò)npm命令來(lái)安裝vue組件和運(yùn)行vue項(xiàng)目,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05

