Home Web Front-end Vue.js How to implement chat interface in vuejs

How to implement chat interface in vuejs

Sep 24, 2021 pm 04:43 PM
vuejs

How to implement the chat interface in vuejs: 1. Install dependencies by executing "npm install"; 2. Implement scrolling loading of data through "scrollLoader.vue"; 3. Modify main.js; 4. Set App.vue The parameters in can be used.

How to implement chat interface in vuejs

The operating environment of this article: Windows 7 system, vue version 2.9.6, DELL G3 computer.

How to implement chat interface in vuejs?

Vue.js imitation WeChat chat window display component function

Source code: https://github.com/doterlin/vue-wxChat

Demo address: https://doterlin.github.io/vue-wxChat/

Run

# install dependencies
npm install
# serve with hot reload at localhost:8080
npm run dev
# build for production with minification
npm run build
Copy after login

Introduction

  • supports the display of text and pictures (the display of voice categories will be supported in the future).
  • Supports scrolling loading of data, in which scrolling loading relies on another component of mine, scrollLoader.vue ("Vue.js up and down scrolling loading component").
  • Support QQ emoticons. In order to use emoticons, please register the command v-emotion globally. I implemented it in main.js; the code is as follows:
function toEmotion(text, isNoGif){
 var list = ['微笑', '撇嘴', '色', '发呆', '得意', '流泪', '害羞', '闭嘴', '睡', '大哭', '尴尬', '发怒', '调皮', '呲牙', '惊讶', '难过', '酷', '冷汗', '抓狂', '吐', '偷笑', '愉快', '白眼', '傲慢', '饥饿', '困', '惊恐', '流汗', '憨笑', '大兵', '奋斗', '咒骂', '疑问', '嘘', '晕', '折磨', '衰', '骷髅', '敲打', '再见', '擦汗', '抠鼻', '鼓掌', '糗大了', '坏笑', '左哼哼', '右哼哼', '哈欠', '鄙视', '委屈', '快哭了', '阴险', '亲亲', '吓', '可怜', '菜刀', '西瓜', '啤酒', '篮球', '乒乓', '咖啡', '饭', '猪头', '玫瑰', '凋谢', '示爱', '爱心', '心碎', '蛋糕', '闪电', '炸弹', '刀', '足球', '瓢虫', '便便', '月亮', '太阳', '礼物', '拥抱', '强', '弱', '握手', '胜利', '抱拳', '勾引', '拳头', '差劲', '爱你', 'NO', 'OK', '爱情', '飞吻', '跳跳', '发抖', '怄火', '转圈', '磕头', '回头', '跳绳', '挥手', '激动', '街舞', '献吻', '左太极', '右太极', '嘿哈', '捂脸', '奸笑', '机智', '皱眉', '耶', '红包', '鸡'];
 if (!text) {
  return text;
 }
 text = text.replace(/\[[\u4E00-\u9FA5]{1,3}\]/gi, function(word){
  var newWord = word.replace(/\[|\]/gi,'');
  var index = list.indexOf(newWord);
  var backgroundPositionX = -index * 24
  var imgHTML = '';
  if(index<0){
   return word;
  }
  if (isNoGif) {
   if(index>104){
    return word;
   }
   imgHTML = `<i class="static-emotion" style="background:url(https://res.wx.qq.com/mpres/htmledition/images/icon/emotion/default218877.gif) ${backgroundPositionX}px 0;"></i>`
  } else {
   var path = index>104 ? &#39;/img&#39; : &#39;https://res.wx.qq.com/mpres/htmledition/images/icon&#39;;
   imgHTML = `![](${path}/emotion/${index}.gif)`
  }
  return imgHTML;
 });
 return text;
}
Vue.directive(&#39;emotion&#39;, {
 bind: function (el, binding) {
  el.innerHTML = toEmotion(binding.value)
 }
});
Copy after login

How to use?

The parameters have been described in the component and demonstrated in App.vue:

Parameters and descriptions:

WeChat chat visual component

Depends on the scrollLoader component and the instruction v-emotion (please see main.js for implementation)

Parameters:

width component width, default 450

wrapBg outer parent element background color, default #efefef

maxHeight is the maximum height of the display window, default is 900

contactAvatarUrl friend avatar url

ownerAvatarUrl WeChat owner avatar url

ownerNickname WeChat owner nickname

getUpperData (required) A method to load data when scrolling to the top. The return value must be a Promise object. The structure of resolve is the same as data

getUnderData (required). A method to load data when scrolling to the bottom. The return value is the same as above

data (required) Pass in initialization data, the structure is as follows:

[{
 direction: 2, //为2表示微信主人发出的消息,1表示联系人
 id: 1, //根据这个来排序消息
 type: 1, //1为文本,2为图片
 content: &#39;你好!![呲牙]&#39;, //当type为1时这里是文本消息,当type2为2时这里要存放图片地址;后续会支持语音的显示
 ctime: new Date().toLocaleString() //显示当前消息的发送时间
},
{
 direction: 1,
 id: 2,
 type: 1,
 content: &#39;你也好。[害羞]&#39;,
 ctime: new Date().toLocaleString()
}]
Copy after login

Complete usage example

Effect: https://doterlin.github .io/vue-wxChat/

Code:

//主文件,对wxChat的用法做示例
<template>
<wxChat 
 :data="wxChatData"
 :showShade="false"
 contactNickname="简叔"
 :getUpperData="getUpperData"
 :getUnderData="getUnderData"
 :ownerAvatarUrl="ownerAvatarUrl"
 :contactAvatarUrl="contactAvatarUrl"
 :width="420">
</wxChat>
</template>
<script>
import wxChat from &#39;./components/wxChat.vue&#39;
export default {
 name: &#39;app&#39;,
 data () {
 return {
  upperTimes: 0,
  underTimes: 0,
  upperId: 0,
  underId: 6,
  ownerAvatarUrl: &#39;./src/assets/avatar1.png&#39;,
  contactAvatarUrl: &#39;./src/assets/avatar2.png&#39;,
  wxChatData: [{
  direction: 2,
  id: 1,
  type: 1,
  content: &#39;你好!![呲牙]&#39;,
  ctime: new Date().toLocaleString()
  },
  {
  direction: 1,
  id: 2,
  type: 1,
  content: &#39;你也好。[害羞]&#39;,
  ctime: new Date().toLocaleString()
  },
  {
  direction: 2,
  id: 3,
  type: 1,
  content: &#39;这是我的简历头像:&#39;,
  ctime: new Date().toLocaleString()
  },
  {
  direction: 2,
  id: 4,
  type: 2,
  content: &#39;./src/assets/wyz.jpg&#39;,
  ctime: new Date().toLocaleString()
  },
  {
  direction: 1,
  id: 5,
  type: 1,
  content: &#39;你开心就好。[微笑]&#39;,
  ctime: new Date().toLocaleString()
  }]
 }
 },
 components:{wxChat},
 methods:{
 //向上滚动加载数据
 getUpperData(){
  var me = this;
  // 这里为模拟异步加载数据
  // 实际上你可能要这么写:
  // return axios.get(&#39;xxx&#39;).then(function(result){
  //  return result; //result的格式需要类似下面resolve里面的数组
  // })
  return new Promise(function(resolve){
  setTimeout(function(){
   //模拟加载完毕
   if(me.upperTimes>3){
   return resolve([]);
   }
   //加载数据
   resolve([{
    direction: 2,
    id: me.upperId-1,
    type: 1,
    content: &#39;向上滚动加载第 &#39; + me.upperTimes +&#39; 条!&#39;,
    ctime: new Date().toLocaleString()
   },
   {
    direction: 1,
    id: me.upperId-2,
    type: 1,
    content: &#39;向上滚动加载第 &#39; + me.upperTimes +&#39; 条!&#39;,
    ctime: new Date().toLocaleString()
   }]
   )
  }, 1000);
  me.upperId= me.upperId+2;
  me.upperTimes++;
  })
 },
 getUnderData(){
  var me = this;
  //意义同getUpperData()
  return new Promise(function(resolve){
  setTimeout(function(){
   //模拟加载完毕
   if(me.underTimes>3){
   return resolve([]);
   }
   //加载数据
   resolve(
   [{
    direction: 1,
    id: me.underId+1,
    type: 1,
    content: &#39;向下滚动加载第 &#39; + me.underTimes +&#39; 条!&#39;,
    ctime: new Date().toLocaleString()
   },
   {
    direction: 2,
    id: me.underId+2,
    type: 1,
    content: &#39;向下滚动加载第 &#39; + me.underTimes +&#39; 条!&#39;,
    ctime: new Date().toLocaleString()
   }]
   )
  }, 1000);
  me.underId = me.underId+2;
  me.underTimes++;
  })
 }
 }
}
</script>
<style>
*{
 margin: 0;
 padding: 0;
}
#app {
 font-family: &#39;Avenir&#39;, Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
 margin-top: 60px;
}
h1, h2 {
 font-weight: normal;
}
ul {
 list-style-type: none;
 padding: 0;
}
li {
 display: inline-block;
}
</style>
Copy after login

Welcome to start:

https://github.com/doterlin/vue-wxChat

Recommended: "The latest 5 vue.js video tutorial selections"

The above is the detailed content of How to implement chat interface in vuejs. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Some tips for developing Android applications using Vue.js and Kotlin language Some tips for developing Android applications using Vue.js and Kotlin language Jul 31, 2023 pm 02:17 PM

Some tips for developing Android applications using Vue.js and Kotlin language. With the popularity of mobile applications and the continuous growth of user needs, the development of Android applications has attracted more and more attention from developers. When developing Android apps, choosing the right technology stack is crucial. In recent years, Vue.js and Kotlin languages ​​have gradually become popular choices for Android application development. This article will introduce some techniques for developing Android applications using Vue.js and Kotlin language, and give corresponding code examples. 1. Set up the development environment at the beginning

Some tips for developing data visualization applications using Vue.js and Python Some tips for developing data visualization applications using Vue.js and Python Jul 31, 2023 pm 07:53 PM

Some tips for developing data visualization applications using Vue.js and Python Introduction: With the advent of the big data era, data visualization has become an important solution. In the development of data visualization applications, the combination of Vue.js and Python can provide flexibility and powerful functions. This article will share some tips for developing data visualization applications using Vue.js and Python, and attach corresponding code examples. 1. Introduction to Vue.js Vue.js is a lightweight JavaScript

Integration of Vue.js and Lua language, best practices and experience sharing in building front-end engines for game development Integration of Vue.js and Lua language, best practices and experience sharing in building front-end engines for game development Aug 01, 2023 pm 08:14 PM

The integration of Vue.js and Lua language, best practices and experience sharing for building a front-end engine for game development Introduction: With the continuous development of game development, the choice of game front-end engine has become an important decision. Among these choices, the Vue.js framework and Lua language have become the focus of many developers. As a popular front-end framework, Vue.js has a rich ecosystem and convenient development methods, while the Lua language is widely used in game development because of its lightweight and efficient performance. This article will explore how to

Integrating Vue.js with Objective-C, tips and advice for developing reliable Mac apps Integrating Vue.js with Objective-C, tips and advice for developing reliable Mac apps Jul 30, 2023 pm 03:01 PM

Integration of Vue.js and Objective-C language, tips and suggestions for developing reliable Mac applications. In recent years, with the popularity of Vue.js in front-end development and the stability of Objective-C in Mac application development, developers Start trying to combine the two to develop more reliable and efficient Mac applications. This article will introduce some tips and suggestions to help developers correctly integrate Vue.js and Objective-C and develop high-quality Mac applications. one

How to use PHP and Vue.js to implement data filtering and sorting functions on charts How to use PHP and Vue.js to implement data filtering and sorting functions on charts Aug 27, 2023 am 11:51 AM

How to use PHP and Vue.js to implement data filtering and sorting functions on charts. In web development, charts are a very common way of displaying data. Using PHP and Vue.js, you can easily implement data filtering and sorting functions on charts, allowing users to customize the viewing of data on charts, improving data visualization and user experience. First, we need to prepare a set of data for the chart to use. Suppose we have a data table that contains three columns: name, age, and grades. The data is as follows: Name, Age, Grades Zhang San 1890 Li

How to use Vue to implement QQ-like chat bubble effects How to use Vue to implement QQ-like chat bubble effects Sep 20, 2023 pm 02:27 PM

How to use Vue to implement QQ-like chat bubble effects In today’s social era, the chat function has become one of the core functions of mobile applications and web applications. One of the most common elements in the chat interface is the chat bubble, which can clearly distinguish the sender's and receiver's messages, effectively improving the readability of the message. This article will introduce how to use Vue to implement QQ-like chat bubble effects and provide specific code examples. First, we need to create a Vue component to represent the chat bubble. The component consists of two main parts

Develop efficient web crawlers and data scraping tools using Vue.js and Perl languages Develop efficient web crawlers and data scraping tools using Vue.js and Perl languages Jul 31, 2023 pm 06:43 PM

Use Vue.js and Perl languages ​​to develop efficient web crawlers and data scraping tools. In recent years, with the rapid development of the Internet and the increasing importance of data, the demand for web crawlers and data scraping tools has also increased. In this context, it is a good choice to combine Vue.js and Perl language to develop efficient web crawlers and data scraping tools. This article will introduce how to develop such a tool using Vue.js and Perl language, and attach corresponding code examples. 1. Introduction to Vue.js and Perl language

Integration of Vue.js and Dart language, practical and development skills for building cool mobile application UI interfaces Integration of Vue.js and Dart language, practical and development skills for building cool mobile application UI interfaces Aug 02, 2023 pm 03:33 PM

Integration of Vue.js and Dart language, practice and development skills for building cool mobile application UI interfaces Introduction: In mobile application development, the design and implementation of the user interface (UI) is a very important part. In order to achieve a cool mobile application interface, we can integrate Vue.js with the Dart language, and use the powerful data binding and componentization features of Vue.js and the rich mobile application development library of the Dart language to build Stunning mobile application UI interface. This article will show you how to

See all articles