vue实现简单在线聊天

引用mui的ui库,ES6的 fetch做网络请求

//html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
<title>vue-chat</title>
<link href="css/mui.min.css" rel="stylesheet"/>
<link rel="stylesheet" type="text/css" href="css/app.css"/>
</head>
<body>
<header class="mui-bar mui-bar-nav">
<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
<h1 class="mui-title">chat (聊天窗口)</h1>
</header>
<div class="mui-content">
<div id='fh-chat'>
<chat-view ></chat-view>
</div>
</div>
<script src="js/vue.js"></script>
<script src="js/fh-chat.js"></script>
</body>
</html>
// js
const ChatView = Vue.component('chat-view', {
render: function (h) {
var that = this;
return h('div', [
h('ul', {'class': 'msg-list'},
Array.apply(null, that.fhchatdata).map(function (item) {
return h('li', {'class': ['msg-item', item.type === 'send' ? 'msg-item-self' : '']},
[
h('i', {'class': ['mui-icon', item.type === 'send' ? 'msg-user mui-icon-person' : 'msg-user mui-icon-chat']}),
h('div', {'class': 'msg-content'}, [
item.value, h('span', {'class': 'msg-content-arrow'})
])
]
)
})
),
h('footer', {'class': 'chat-footer'}, [
h('input', {
'class': 'input-text',
domProps: {
value: that.newSend
},
on: {
input: function (e) {
that.newSend = e.target.value
},
change: this.toggleIcon
}
}),
h('span', {
'class': ['mui-icon', this.vsend ? 'mui-icon-compose' : 'mui-icon-paperplane'],
on: {
click: this.send
},
}),
])
])
},
data(){
return {
vsend: true,
newSend: '',
fhchatdata: []
}
},
methods: {
toggleIcon(){
this.vsend = !this.vsend
},
send(){
if (this.newSend) {
this.fhchatdata.push({type: 'send', value: this.newSend});
// this.fh_fetch('xurl', 'POST', {type: 'send', value: this.newSend})
}
this.newSend = '';
},
fh_fetch (url = '', type = 'GET', data = {}){
// url = baseUrl + url;
let requestObj = {
credentials: 'include',
method: type,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
mode: "no-cors",
};
if (type == 'GET') {
let dataStr = '';
Object.keys(data).forEach(key => {
dataStr += key + '=' + data[key] + '&';
});
if (dataStr !== '') {
dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
url = url + '?' + dataStr;
}
} else if (type == 'POST') {
Object.defineProperty(requestObj, 'body', {
value: JSON.stringify(data)
})
} else {
console.log('error')
}
fetch(url, requestObj)
.then(res => {
if (res.status == 200) {
return res.json()
} else {
console.log('error:' + res.status)
}
})
.then(json=> {
this.fhchatdata = json
})
.catch(err=>console.log('error:' + err))
}
},
mounted(){
this.fh_fetch('js/chatData.json')
}
}); new Vue({
el: '#fh-chat',
});
//css
/*online chat*/
.chat-footer {
position: fixed;
bottom: .1rem;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
width: 100%;
height: 3rem;
padding: .1rem 0;
margin: 0 1px;
font-size: 1.6rem;
line-height: 1;
border-top: solid 1px #bbb;
background-color: #fafafa;
} .chat-footer > .input-text {
-webkit-flex: auto;
flex: auto;
background: #fff;
border: solid 1px #ddd;
} .chat-footer > span{
color: blue;
padding: 2px;
} #fh-chat .msg-list {
height: 100%;
overflow: auto;
-webkit-overflow-scrolling: touch;
margin-bottom: 4.5rem !important;
} .msg-item {
padding: 8px;
clear: both;
} .msg-item .msg-user {
width: 38px;
height: 38px;
border: solid 1px #d3d3d3;
display: inline-block;
background: #fff;
border-radius: 3px;
vertical-align: top;
text-align: center;
float: left;
padding: 3px;
color: #ddd;
} .msg-item .msg-content {
display: inline-block;
border-radius: 5px;
border: solid 1px #d3d3d3;
background-color: #FFFFFF;
color: #333;
padding: 8px;
vertical-align: top;
font-size: 15px;
position: relative;
margin: 0 8px;
max-width: 75%;
min-width: 35px;
float: left;
} .msg-item .msg-content .msg-content-inner {
overflow-x: hidden;
} .msg-item .msg-content .msg-content-arrow {
position: absolute;
border: solid 1px #d3d3d3;
border-right: none;
border-top: none;
background-color: #FFFFFF;
width: 10px;
height: 10px;
left: -5px;
top: 12px;
-webkit-transform: rotateZ(45deg);
transform: rotateZ(45deg);
} .msg-item-self .msg-user,
.msg-item-self .msg-content {
float: right;
} .msg-item-self .msg-content .msg-content-arrow {
left: auto;
right: -5px;
-webkit-transform: rotateZ(225deg);
transform: rotateZ(225deg);
} .msg-item-self .msg-content,
.msg-item-self .msg-content .msg-content-arrow {
background-color: #4CD964;
color: #fff;
border-color: #2AC845;
}

vue实现简单在线聊天的更多相关文章

  1. SpringBoot+Vue+WebSocket 实现在线聊天

    一.前言 本文将基于 SpringBoot + Vue + WebSocket 实现一个简单的在线聊天功能 页面如下: 在线体验地址:http://www.zhengqingya.com:8101 二 ...

  2. java Socket实现简单在线聊天(二)

    接<java Socket实现简单在线聊天(一)>,在单客户端连接的基础上,这里第二步需要实现多客户端的连接,也就需要使用到线程.每当有一个新的客户端连接上来,服务端便需要新启动一个线程进 ...

  3. 基于Server-Sent Event的简单在线聊天室

    Web即时通信 所谓Web即时通信,就是说我们可以通过一种机制在网页上立即通知用户一件事情的发生,是不需要用户刷新网页的.Web即时通信的用途有很多,比如实时聊天,即时推送等.如当我们在登陆浏览知乎时 ...

  4. java Socket实现简单在线聊天(三)

    在上一篇,利用线程使服务端实现了能够接收多客户端请求的功能,这里便需要客户端接收多客户端消息的同时还能把消息转发到每个连接的客户端,并且客户端要能在内容显示区域显示出来,从而实现简单的在线群聊. 在实 ...

  5. java Socket实现简单在线聊天(一)

    最近的项目有一个在线网页交流的需求,由于很久以前做过的demo已经忘记的差不多了,因此便重新学习一下. 我计划的大致实现步骤分这样几大步: 1.使用awt组件和socket实现简单的单客户端向服务端持 ...

  6. 基于Server-Sent Event的简单聊天室 Web 2.0时代,即时通信已经成为必不可少的网站功能,那实现Web即时通信的机制有哪些呢?在这门项目课中我们将一一介绍。最后我们将会实现一个基于Server-Sent Event和Flask简单的在线聊天室。

    基于Server-Sent Event的简单聊天室 Web 2.0时代,即时通信已经成为必不可少的网站功能,那实现Web即时通信的机制有哪些呢?在这门项目课中我们将一一介绍.最后我们将会实现一个基于S ...

  7. vue.js+socket.io+express+mongodb打造在线聊天

    vue.js+socket.io+express+mongodb打造在线聊天 在线地址观看 http://www.chenleiming.com github地址 https://github.com ...

  8. vue.js+socket.io+express+mongodb打造在线聊天[二]

    vue.js+socket.io+express+mongodb打造在线聊天[二] 在线地址观看 http://www.chenleiming.com github地址 https://github. ...

  9. 基于PHP实现一个简单的在线聊天功能(轮询ajax )

    基于PHP实现一个简单的在线聊天功能(轮询ajax ) 一.总结 1.用的轮询ajax 二.基于PHP实现一个简单的在线聊天功能 一直很想试着做一做这个有意思的功能,感觉复杂的不是数据交互和表结构,麻 ...

随机推荐

  1. 题解【luoguP4145 上帝造题的七分钟2(花神游历各国)】

    题目链接 题解 题目大意: 一个序列,支持区间开方与求和操作. 算法:线段树实现开方修改与区间求和 分析: 显然,这道题的求和操作可以用线段树来维护 但是如何来实现区间开方呢 大家有没有这样的经历:玩 ...

  2. scrapy架构设计分析

    scrapy是一个Python爬虫框架.我们自己用requests也能写爬虫(GET某个URL,然后Parse网页的内容),那么,问题来了,scrapy高明在哪些地方呢?下面就来讨论下这个话题,看看业 ...

  3. SNS应用好友动态Feed模块设计

    转载自:http://libo93122.blog.163.com/blog/static/122189382012112145728902/ 备注:找不到原作者了. 现在大部分SNS网站都有一个功能 ...

  4. 关于微信内置浏览器安卓端session丢失问题

    项目上线测试,发现微信安卓端存在用户登录无法验证session情况, 导致每次接口请求都无法识别,而苹果客户端不会出现此问题,非微信环境打开不会出现此问题,找到一些解决方案做下记录: 方案1: 由于微 ...

  5. 【asp.net mvc】 扩展 htmlhelper 实现分页

    参考文档:http://www.cnblogs.com/caofangsheng/p/5670071.html                  http://www.cnblogs.com/arte ...

  6. Java 中 给静态方法 添加泛型 (static <T>)

    今天在用到static方法的时候.想要用泛型.结果不能通过编译. 上网查了一下.其具体写法如下:

  7. Ubuntu12.04 安装nginx和mongo过程

    1.安装php和php-cgi apt-get install php5 php5-cgi 2.安装 nginx apt-get install nginx 3.安装 MongoDB apt-get ...

  8. 【BZOJ4821】【SDOI2017】相关分析 [线段树]

    相关分析 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Frank对天文学非常感兴趣,他经 ...

  9. 【LibreOJ】#539. 「LibreOJ NOIP Round #1」旅游路线

    [题意]给定正边权有向图,车油量上限C,每个点可以花费pi加油至min(C,ci),走一条边油-1,T次询问s点出发带钱q,旅行路程至少为d的最多剩余钱数. n<=100,m<=1000, ...

  10. 关于scala 集合 List Map Set

    1,数组 2,List,ListBuffer 3, Map , mutable.Map