用Vue的代码写一个通讯软件界面
时间: 2023-05-14 09:07:04 浏览: 89
用Vue开发小程序的框架
非常感谢您的提问。以下是一个简单的Vue通讯软件界面的代码示例:
```
<template>
<div class="chat-app">
<div class="chat-header">
<h1>Chat App</h1>
</div>
<div class="chat-body">
<div class="chat-sidebar">
<ul>
<li v-for="user in users" :key="user.id">
<img :src="user.avatar" alt="user avatar">
<span>{{ user.name }}</span>
</li>
</ul>
</div>
<div class="chat-main">
<div class="chat-messages">
<div v-for="message in messages" :key="message.id">
<div v-if="message.sender === currentUser.id" class="message sent">
<p>{{ message.content }}</p>
</div>
<div v-else class="message received">
<p>{{ message.content }}</p>
</div>
</div>
</div>
<div class="chat-input">
<input type="text" v-model="newMessage" @keyup.enter="sendMessage">
<button @click="sendMessage">Send</button>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
currentUser: {
id: 1,
name: 'John Doe',
avatar: 'https://via.placeholder.com/50x50',
},
users: [
{
id: 2,
name: 'Jane Smith',
avatar: 'https://via.placeholder.com/50x50',
},
{
id: 3,
name: 'Bob Johnson',
avatar: 'https://via.placeholder.com/50x50',
},
],
messages: [
{
id: 1,
sender: 2,
content: 'Hello John!',
},
{
id: 2,
sender: 1,
content: 'Hi Jane!',
},
],
newMessage: '',
};
},
methods: {
sendMessage() {
if (this.newMessage.trim() !== '') {
const newId = this.messages.length + 1;
this.messages.push({
id: newId,
sender: this.currentUser.id,
content: this.newMessage,
});
this.newMessage = '';
}
},
},
};
</script>
<style>
.chat-app {
display: flex;
flex-direction: column;
height: 100%;
}
.chat-header {
background-color: #333;
color: #fff;
padding: 1rem;
text-align: center;
}
.chat-body {
display: flex;
flex-grow: 1;
}
.chat-sidebar {
background-color: #eee;
padding: 1rem;
width: 20%;
}
.chat-main {
display: flex;
flex-direction: column;
flex-grow: 1;
padding: 1rem;
}
.chat-messages {
flex-grow: 1;
overflow-y: auto;
}
.message {
margin-bottom: 1rem;
padding: 0.5rem;
border-radius: 0.5rem;
max-width: 80%;
}
.sent {
background-color: #007bff;
color: #fff;
align-self: flex-end;
}
.received {
background-color: #eee;
align-self: flex-start;
}
.chat-input {
display: flex;
margin-top: 1rem;
}
.chat-input input {
flex-grow: 1;
margin-right: 1rem;
padding: 0.5rem;
border-radius: 0.5rem;
border: none;
}
.chat-input button {
padding: 0.5rem;
border-radius: 0.5rem;
border: none;
background-color: #007bff;
color: #fff;
}
</style>
```
这是一个基本的聊天应用程序界面,包括一个侧边栏显示在线用户,一个主聊天窗口显示消息历史记录和一个输入框允许用户发送新消息。这个示例使用Vue框架编写,并使用了一些基本的CSS样式来布局和样式化界面。
阅读全文