vue實(shí)現(xiàn)聊天框發(fā)送表情
vue聊天框發(fā)送表情以及vue界面發(fā)送表情實(shí)現(xiàn)的具體代碼,供大家參考,具體內(nèi)容如下
1.在發(fā)送消息的時(shí)候,判斷發(fā)送的消息是不是表情,表情的type:3,content:[愛心],這樣存儲(chǔ)在數(shù)據(jù)庫中
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è)來判斷發(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è)長連接
import _ from "lodash";//這個(gè)是js一個(gè)很強(qiáng)大的庫
import eventBus from '@/eventBus'//這是一個(gè)子父傳遞的公共文件
console.log(emoji)
export default {
data() {
this.imgs = _.chunk(emoji, 6) //這個(gè)是調(diào)用lodash庫的chunk方法 把 六個(gè)元素分成一個(gè)數(shù)組只不過是emoji這個(gè)數(shù)組中的二維數(shù)組
return {
emojiShow: false //剛開始默認(rèn)不顯示 點(diǎn)擊按鈕顯示 點(diǎn)擊的按鈕上可以寫@click='emojiShow=emojiShow'這種寫法
};
},
methods: {
customEmoji(text) { //這個(gè)函數(shù)就是服務(wù)器端把傳過來的名稱轉(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}]`//傳過來的名字轉(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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue動(dòng)態(tài)綁定class的幾種常用方式小結(jié)
這篇文章主要介紹了vue動(dòng)態(tài)綁定class的幾種常用方式,結(jié)合實(shí)例形式總結(jié)分析了vue.js常見的對象方法、數(shù)組方法進(jìn)行class動(dòng)態(tài)綁定相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
vue3中通過ref獲取元素節(jié)點(diǎn)的實(shí)現(xiàn)
這篇文章主要介紹了vue3中通過ref獲取元素節(jié)點(diǎn)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
詳解基于 axios 的 Vue 項(xiàng)目 http 請求優(yōu)化
這篇文章主要介紹了詳解基于 axios 的 Vue 項(xiàng)目 http 請求優(yōu)化,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
Vue中使用localStorage存儲(chǔ)token并設(shè)置時(shí)效
這篇文章主要為大家介紹了Vue中使用localStorage存儲(chǔ)token并設(shè)置時(shí)效,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Vue3中的shallowRef?和shallowReactive對比分析
這篇文章主要介紹了Vue3中的shallowRef?和shallowReactive,通過示例代碼逐一對他們的使用做的詳細(xì)介紹,文末補(bǔ)充介紹了vue3的shallowRef()、shallowReactive()和shallowReadonly()的使用,需要的朋友可以參考下2023-01-01

