美文网首页测试员的那点事
vue-微信聊天界面(二)

vue-微信聊天界面(二)

作者: 望月成三人 | 来源:发表于2020-11-08 21:53 被阅读0次
  • 上编是微信界面(一),界面如下
    image.png
  • 这篇文章的目标页面如下:


    image.png
  • 在输入框输入内容,马上再页面上展示

代码

  • 左侧的聊天记录展示界面组件代码(ChatWindow.vue
<template>
    <div class="chat">
        <h3>用户聊天界面</h3>
        <p>用户:{{currentUser.username}}</p>
        <div class="chatlist">
            <!--聊天记录内容列表展示 -->
            <chatItemCom v-for="(item,index) in chatlist" :key="'chatlist'+index" :chatitem="item">
                <!-- 传给插槽的内容-->
                {{item.chatcontent}}
            </chatItemCom>
        </div>
        <!--输入内容组件,绑定父元素的发送方法,触发后就是子组件发送内容给父组件-->
        <ChatInputCom :sendEvent="sendEvent"></ChatInputCom>
    </div>
</template>

<style scoped>
    .chat 
    {
        width: 500px;
    }

</style>


<script>

// 输入内容,发送内容组件
import ChatInputCom from './ChatInputCom'
// 输入内容后展示内容后的组件
import chatItemCom from './chatItemCom'


export default {
    components: {
        ChatInputCom,chatItemCom
    },
    data() {
        return {
            chatlist:[
                // {
                //     user:{
                //         username: "小明",
                //         headimg:require("../assets/images/1.jpg"),
                //         },
                //         chatcontent: "你好啊"
                //     }
            ]
        }
    },
    props:["currentUser"],
    methods:{
        // 父元素传给子组件ChatInputCom,子组件触发父元素的方法,
        //然后把chatcontent的值传给父元素,父元素把内容新增到chatlist中,chatlist就是内容展示组件chatItemCom的数据列表
        sendEvent:function(value){
            this.chatlist.push({
                 user:{
                        username: "小明",
                        headimg:require("../assets/images/1.jpg"),
                        },
                        chatcontent: value
                    }
                
            )
        }
    }
}
</script>
  • ChatInputCom输入内容并发送组件
<!-- 发送消息组件-->
<template>
    <div class="chatinput">
        <input type="text"  @keydown.enter="send" v-model="inputContent" />
        <!-- <input type="text" @keydown.enter="sendEvent($event.target.value)" v-model="inputContent" /> -->

        <button @click="send">发送</button>
    </div>
</template>

<script>
export default {
    data() {
        return {
            inputContent:"",
        }
    },
  //绑定父元素传过来的方法
    props:["sendEvent"],
    methods:{
        send:function(){
            this.sendEvent(this.inputContent)
            this.inputContent = ""
        }
    }
}
</script>
  • chatItemCom.vue 聊天内容列表展示组件
<!--聊天内容记录-->
<template>
    <div class="chatitem">
       <!-- 注意style绑定的知识点-->
        <img :style="imgStyle" :src="chatitem.user.headimg" alt="头像" />
        <div>
        <!--用插槽展示内容-->
            <slot></slot>
        </div>
    </div>
</template>
<style scoped>
    /* .chatitem img{width:80;height: 80px;} */
    .chatitem {display: flex;}
</style>
<script>
export default {
    data() {
        return {
            imgStyle:{
                 width: "60px",
                 height:"60px"
                }
        }
    },
    props:["chatitem"]
}
</script>

相关文章

  • vue-微信聊天界面(二)

    上编[https://www.jianshu.com/p/76ae4cd1dfc5]是微信界面(一),界面如下im...

  • vue-微信聊天界面(一)

    经过前面的基础学习,现在开始学习使用脚手架 本节目标界面如下image.png 点击右侧用户列表,左侧聊天界面展示...

  • html5仿微信web版|仿微信客户端聊天

    h5仿微信实战项目|仿微信web端聊天界面|仿微信电脑端界面 之前做了一个手机端 h5仿微信聊天 界面,最近又开发...

  • iXShot样张

    测试机型iPhone SE 1.微信朋友圈支持朋友圈界面的拼接 2.微信好友列表 3.微信聊天界面2 4.微信聊天...

  • 微信小助手

    批量添加标签 微信聊天界面,点击内容,选择更多,选中同类标签内容,标注标签。 常用微信功能界面 聊天文件查找 设为...

  • 微信搜索长按文字方案

    微信聊天界面长按文字搜索方案 一、当前版本概况 当前版本的微信版本在聊天界面不能长按文字直接对文字进行搜索的功能。...

  • iOS类似微信聊天界面时间转换

    iOS类似微信聊天界面时间转换 下面这个大家不用看,仅供参考,类似微信聊天界面每隔5分钟显示一次时间

  • 微信聊天界面分析

    一直以来,用我们的app聊天有种奇怪的感觉,但具体哪里奇怪,也说不上来。直到开发iPad版,遇到各种问题,仔细研究...

  • 微信界面

    随着微信的普及,微信聊天也成为了人们每天必不可少的活动。打开微信首先引入眼帘的是微信聊天界面,而每个人也有着自己的...

  • h5聊天室weChatRoom|仿微信群聊天室(多人聊天)

    html5+css3聊天室实战开发|h5仿微信聊天室界面|仿微信群聊/单聊 这个项目是利用h5开发的仿微信聊天室,...

网友评论

    本文标题:vue-微信聊天界面(二)

    本文链接:https://www.haomeiwen.com/subject/ymqkbktx.html