vue實現(xiàn)聊天框發(fā)送表情
更新時間:2021年08月31日 14:19:43 作者:MYG_G
這篇文章主要為大家詳細介紹了vue實現(xiàn)聊天框發(fā)送表情,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
vue聊天框發(fā)送表情以及vue界面發(fā)送表情實現(xiàn)的具體代碼,供大家參考,具體內(nèi)容如下
1.在發(fā)送消息的時候,判斷發(fā)送的消息是不是表情,表情的type:3,content:[愛心],這樣存儲在數(shù)據(jù)庫中
2.在獲取聊天記錄的時候,判斷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) // }, // 時間戳處理 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ā)送表情實現(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ù)這個來判斷發(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"; //是一個長連接 import _ from "lodash";//這個是js一個很強大的庫 import eventBus from '@/eventBus'//這是一個子父傳遞的公共文件 console.log(emoji) export default { data() { this.imgs = _.chunk(emoji, 6) //這個是調(diào)用lodash庫的chunk方法 把 六個元素分成一個數(shù)組只不過是emoji這個數(shù)組中的二維數(shù)組 return { emojiShow: false //剛開始默認不顯示 點擊按鈕顯示 點擊的按鈕上可以寫@click='emojiShow=emojiShow'這種寫法 }; }, methods: { customEmoji(text) { //這個函數(shù)就是服務器端把傳過來的名稱轉(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
Vue中實現(xiàn)父子組件雙向數(shù)據(jù)流的三種方案分享
通常情況下,父子組件的通信都是單向的,或父組件使用props向子組件傳遞數(shù)據(jù),或子組件使用emit函數(shù)向父組件傳遞數(shù)據(jù),本文將嘗試講解Vue中常用的幾種雙向數(shù)據(jù)流的使用,需要的朋友可以參考下2023-08-08Vue發(fā)送Formdata數(shù)據(jù)及NodeJS接收方式
這篇文章主要介紹了Vue發(fā)送Formdata數(shù)據(jù)及NodeJS接收方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06vue中實現(xiàn)子組件相互切換且數(shù)據(jù)不丟失的策略詳解
項目為數(shù)據(jù)報表,但是一個父頁面中有很多的子頁面,而且子頁面中不是相互關聯(lián),但是數(shù)據(jù)又有聯(lián)系,所以本文給大家介紹了vue中如何實現(xiàn)子組件相互切換,而且數(shù)據(jù)不會丟失,并有詳細的代碼供大家參考,需要的朋友可以參考下2024-03-03