Vue实现留言板功能

一、继上次使用ElementUI实现了留言板,这次不使用组件库再实现一次

二、为什么要录制一个丑陋版本?

答:这个版本具有高度的可定制化,相比使用组件库,样式更灵活;而且实现更为简单,适合期末大作业。

页面效果:

代码实现:

msgBlock.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>index</title>
    <script src="../js/vue.js"></script>
    <link rel="stylesheet" href="msgBlock.css">
</head>

<body>
<div id="message_word">

    <h1>留言</h1>

    <div id="message_word_div1">
        <table id="message_word_table">
            <tr>
                <th>姓名</th>
                <th>手机号</th>
                <th>留言内容</th>
                <th>操作</th>
            </tr>

            <tr v-for="(comment,idx) in comments" :key="idx">
                <td>{{comment.name}}</td>
                <td>{{comment.phoneID}}</td>
                <td>{{comment.comment}}</td>
                <td>
                    <button @click="delThis(idx)">
                        删除
                    </button>
                </td>
            </tr>
        </table>
    </div>


    <div id="message_word_div2">
        <form action=""><br>
            留言者&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text"
                                                                                                v-model="name">
            <br><br>
            手机号&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<input type="text" v-model="phoneID">
            <br><br>
            留言内容<br><br><textarea name="name" cols="30" rows="8" v-model="comment"></textarea>
            <button type="button" @click="commit">
                提交
            </button>
        </form>
    </div>

</div>
</body>

<script src="msgBlock.js"></script>
</html>

msgBlock.js

const vm = new Vue({
    data: {
        name: "",
        phoneID: "",
        comment: "",
        comments: [],
    },
    methods: {
        commit(e) {
            e.preventDefault();
            const cm = this.comments;

            cm.push({
                name: this.name,
                phoneID: this.phoneID,
                comment: this.comment,
            });

            this.name = "";
            this.phoneID = "";
            this.comment = "";
            this.comments = cm;
        },
        delThis(index) {
            this.comments.splice(index, 1);
        },
    },
});

vm.$mount('#message_word')

msgBlock.css

#message_word {
	margin: auto;
	margin-left: 185px;
	width: 1200px;
	height: 500px;
}

#message_word h1 {
	font-size: 45px;
	margin: auto;
	margin-top: 50px;
	margin-left: 30px;
	width: 100px;
}

#message_word_div1 {
	width: 800px;
	height: 100px;
	margin: auto;
	margin-top: -10px;
	margin-left: 400px;
}

#message_word_div2 form {
	font-size: 20px;
	margin: auto;
}

#message_word_div2 form textarea{
	font-size: 15px;
	margin: auto;
	margin-left: 20px;
	margin-top: -10px;
}

#message_word_div2 form button {
	background-color: blue;
	font-size: 25px;
	color: white;
	border-radius: 15px;
	margin-left: 70px;
	width: 150px;
}

#message_word_div2 {
	width: 300px;
	height: 400px;
	margin: auto;
	margin-left: 30px;
	margin-top: -50px;
	border: 3px solid black;
}

#message_word_div1 table {
	font-size: 18px;
}
#message_word_div1 table tr td button {
	font-size: 20px;
	color: white;
	border: 1px solid black;
	background-color: red;
	border-radius: 5px;
}

#message_word_div1 table th {
	width: 200px;
}

#message_word_div1 table tr td {
	text-align: center;
}

  • 13
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Vue和Element UI实现留言板的示例代码: 1. 首先,确保已经安装了Element UI库。可以使用以下命令进行安装: ```shell npm i element-ui -S ``` 2. 在main.js文件中引入Element UI并注册组件: ```javascript import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' Vue.use(ElementUI) ``` 3. 创建一个留言板组件,例如MessageBoard.vue: ```vue <template> <div> <el-card> <div slot="header">留言板</div> <el-infinite-scroll @load="loadMore" :distance="10"> <el-timeline> <el-timeline-item v-for="message in messages" :key="message.id"> <template v-slot:timestamp> {{ message.time }} </template> {{ message.content }} </el-timeline-item> </el-timeline> </el-infinite-scroll> </el-card> </div> </template> <script> export default { data() { return { messages: [], // 存储留言数据 page: 1, // 当前页码 pageSize: 10, // 每页显示数量 } }, mounted() { this.loadMore() }, methods: { loadMore() { // 模拟异步加载留言数据 setTimeout(() => { // 假设从后端获取到的留言数据为response const response = [ { id: 1, content: '留言1', time: '2021-01-01 10:00:00' }, { id: 2, content: '留言2', time: '2021-01-02 12:00:00' }, // ... ] this.messages = this.messages.concat(response) this.page++ }, 500) }, }, } </script> <style scoped> /* 样式可以根据需求进行自定义 */ </style> ``` 4. 在需要使用留言板的页面中引入MessageBoard组件: ```vue <template> <div> <!-- 其他页面内容 --> <message-board></message-board> </div> </template> <script> import MessageBoard from '@/components/MessageBoard.vue' export default { components: { MessageBoard, }, } </script> ``` 这样就实现了一个简单的留言板功能留言板会根据滚动加载的方式,每次加载10条留言数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值