用vue3实现留言板功能

效果图:

代码:

html 复制代码
<script setup lang="ts">
import { ref } from 'vue';

interface Message {
  name: string;
  phone: string;
  message: string;
}

const name = ref<string>('');
const phone = ref<string>('');
const message = ref<string>('');
const messages = ref<Message[]>([]);

const submitMessage = () => {
  if (name.value && phone.value && message.value) {
    const phoneRegex = /^1[0123456789]\d{9}$/;
    if (!phoneRegex.test(phone.value)) {
      alert('请输入正确的手机号!');
      return;
    }
    messages.value.unshift({
      name: name.value,
      phone: phone.value,
      message: message.value
    });
    name.value = '';
    phone.value = '';
    message.value = '';
  } else {
    alert('请填写完整的信息!');
  }
};

const deleteMessage = (index: number) => {
  messages.value.splice(index, 1);
};

const editMessage = (index: number) => {
  const editedMessage = prompt('请输入编辑后的留言内容:', messages.value[index].message);
  if (editedMessage !== null) {
    messages.value[index].message = editedMessage;
  }
};
</script>

<template>
  <div id="app">
    <h1>留言板</h1>
    <div class="message-form">
      <label for="name">留言者姓名:</label>
      <input type="text" v-model="name" placeholder="留言者姓名">
      <label for="phone">手机号:</label>
      <input type="text" v-model="phone" placeholder="手机号">
      <label for="message">留言内容:</label>
      <textarea v-model="message" placeholder="留言内容"></textarea>
      <button @click="submitMessage">提交</button>
    </div>

    <div class="message-list" v-if="messages.length > 0">
      <div v-for="(msg, index) in messages" :key="index" class="message">
        <div>留言者姓名:{{ msg.name }}</div>
        <div>手机号:{{ msg.phone }}</div>
        <div>留言内容:{{ msg.message }}</div>
        <button @click="deleteMessage(index)">删除</button>
        <button @click="editMessage(index)">编辑</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
#app {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Roboto', sans-serif;
}

h1 {
  text-align: center;
  margin-bottom: 20px;
  color: #3498db;
}

.message-form {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  margin-bottom: 20px;
}
.message-form label {
  width: 100%; 
  margin-bottom: 10px;
}

.message-form input,
.message-form textarea {
  width: 100%;
  padding: 10px;
  margin-bottom: 15px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
}

.message-form button {
  padding: 10px 20px;
  background-color: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
  display: block;
  margin: 0 auto;
}

.message-form button:hover {
  background-color: #2980b9;
}
.message-list {
  margin-top: 20px;
}

.message {
  background-color: #eef5f9;
  border-left: 4px solid #007bff;
  padding: 15px;
  margin-bottom: 10px;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.message button {
  background-color: transparent;
  color: #007bff;
  border: 1px solid #007bff;
  margin-right: 10px;
  margin-top: 10px;
  transition: background-color 0.3s ease, color 0.3s ease;
}

.message button:hover {
  background-color: #007bff;
  color: white;
}
</style>
相关推荐
mldong7 小时前
你的 Vue3 项目也能有钉钉同款审批流设计器:npm 装包,10 分钟画出第一条审批流
前端·vue.js
2分钟速写快排7 小时前
什么是 RAG?如何用 RAG 实现一个用户记忆?
前端·后端·ai编程
passerby60618 小时前
如何自己造一个时间处理库
前端·javascript·github
走到天涯海角9 小时前
react里面的长列表渲染优化
前端·react.js·前端框架
小羊没烦恼!9 小时前
Hello Web API系列教程——Web API与国际化
java·服务器·前端·javascript·php
北岛贰9 小时前
迷茫焦虑期,我做了一个带支付带官网的 AI 聊天虚拟恋人 App
前端·人工智能·后端
mayaairi11 小时前
Vue2 组件通讯(三):全局事件总线、PubSub、插槽与组件实例属性
前端·javascript·vue.js
kyriewen11 小时前
面试官问我:AI 都能写代码了,前端凭什么还值 25K
前端·javascript·人工智能
风骏时光牛马12 小时前
AI源码分析:拆解模型底层实现逻辑
前端
BillKu12 小时前
全局样式变量:CSS 自定义属性(--border-color)和SCSS 变量($border-color)的说明
javascript·css·scss