Vue組件實(shí)現(xiàn)評(píng)論區(qū)功能
本文實(shí)例為大家分享了Vue組件實(shí)現(xiàn)評(píng)論區(qū)的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)代碼
<!DOCTYPE html>
<html lang="en">
<head>
?? ?<meta charset="utf-8">
?? ?<meta name="viewport" content="width=device-width, inital-scale=1.0">
?? ?<meta http-equiv="X-UA-Compatible" content="ie=edge">
?? ?<title>評(píng)論</title>
?? ?<script src="vue.js"></script>
?? ?<link rel="stylesheet" ?href="bootstrap.min.css" >
</head>
<body>
?? ?<div id="app">
?? ??? ?<cmt-box @func="loadComments"></cmt-box>
?? ??? ?<ul class="list-group">
?? ??? ??? ?<li class="list-group-item" v-for="item in list" :key="item.id">
?? ??? ??? ??? ?<span class="badge">評(píng)論人:{{ item.user }}</span>
?? ??? ??? ??? ?{{ item.content }}
?? ??? ??? ?</li>
?? ??? ?</ul>
?? ?</div>
?? ?<template id="tmpl">
?? ??? ?<div>
?? ??? ??? ?<div class="form-group">
?? ??? ??? ??? ?<label>評(píng)論人:</label>
?? ??? ??? ??? ?<input type="text" class="form-control" v-model="user">?
?? ??? ??? ?</div>
?? ??? ??? ?<div class="form-group">
?? ??? ??? ??? ?<label>評(píng)論內(nèi)容:</label>
?? ??? ??? ??? ?<textarea class="form-control" v-model="content"></textarea>
?? ??? ??? ?</div>
?? ??? ??? ?<div class="form-group">
?? ??? ??? ??? ?<input type="button" value="發(fā)表評(píng)論" class="btn btn-primary" @click="postComments">
?? ??? ??? ?</div>
?? ??? ?</div>
?? ?</template>
?? ?<script>
?? ??? ?var commentBox = {
?? ??? ??? ?template: '#tmpl',
?? ??? ??? ?data() {
?? ??? ??? ??? ?return {
?? ??? ??? ??? ??? ?user: '',
?? ??? ??? ??? ??? ?content: ''?? ?
?? ??? ??? ??? ?}
?? ??? ??? ?},
?? ??? ??? ?methods: {
?? ??? ??? ??? ?postComments() {
?? ??? ??? ??? ??? ?var comment = {id: Date.now(), user:this.user, content:this.content }
?? ??? ??? ??? ??? ?var list = JSON.parse(localStorage.getItem('cmts') || '[]')
?? ??? ??? ??? ??? ?list.unshift(comment)
?? ??? ??? ??? ??? ?localStorage.setItem('cmts',JSON.stringify(list))
?? ??? ??? ??? ??? ?this.user = this.content = ''
?? ??? ??? ??? ??? ?this.$emit('func')
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?var vm = new Vue({
?? ??? ??? ?el: '#app',
?? ??? ??? ?data: {
?? ??? ??? ??? ?list:[
?? ??? ??? ??? ??? ?{ time: Date.now(), user: '路人甲', content: '武漢加油' },
?? ??? ??? ??? ??? ?{ time: Date.now(), user: '炮灰乙', content: '中國(guó)加油' },
?? ??? ??? ??? ??? ?{ time: Date.now(), user: '小兵丙', content: '廣東加油' },
?? ??? ??? ??? ??? ?{ time: Date.now(), user: '土匪丁', content: '全球加油' }
?? ??? ??? ??? ?]
?? ??? ??? ?},
?? ??? ??? ?created(){
?? ??? ??? ??? ?this.loadComments()
?? ??? ??? ?},
?? ??? ??? ?methods: {
?? ??? ??? ??? ?loadComments(){
?? ??? ??? ??? ??? ?var list = JSON.parse(localStorage.getItem('cmts') || '[]')
?? ??? ??? ??? ??? ?this.list = list
?? ??? ??? ??? ?}
?? ??? ??? ?},
?? ??? ??? ?components: {
?? ??? ??? ??? ?'cmt-box': commentBox
?? ??? ??? ?}
?? ??? ?});
?? ?</script>
</body>
</html>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)發(fā)表評(píng)論功能
- vue實(shí)現(xiàn)文章評(píng)論和回復(fù)列表
- VUE+Java實(shí)現(xiàn)評(píng)論回復(fù)功能
- vue開發(fā)實(shí)現(xiàn)評(píng)論列表
- vue實(shí)現(xiàn)評(píng)論列表
- Vue實(shí)現(xiàn)簡(jiǎn)單的發(fā)表評(píng)論功能
- vue實(shí)現(xiàn)評(píng)論列表功能
- Vuepress 搭建帶評(píng)論功能的靜態(tài)博客的實(shí)現(xiàn)
- Vue.js實(shí)現(xiàn)文章評(píng)論和回復(fù)評(píng)論功能
- vue組件實(shí)現(xiàn)發(fā)表評(píng)論功能
相關(guān)文章
Vue 3 + Element Plus 封裝單列控制編輯的可編輯表格
在Web應(yīng)用開發(fā)中實(shí)現(xiàn)表格數(shù)據(jù)編輯功能至關(guān)重要,本文將詳細(xì)介紹如何使用Vue3和ElementPlus庫(kù)來構(gòu)建一個(gè)支持單列編輯的表格組件,本教程詳細(xì)闡述了組件創(chuàng)建過程和數(shù)據(jù)綁定機(jī)制,幫助你快速掌握Vue3中表格編輯功能的實(shí)現(xiàn),感興趣的朋友一起看看吧2024-09-09
vue-router 按需加載 component: () => import() 報(bào)錯(cuò)的解決
這篇文章主要介紹了vue-router 按需加載 component: () => import() 報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
elementui中el-input回車搜索實(shí)現(xiàn)示例
這篇文章主要介紹了elementui中el-input回車搜索實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
在 Vue3 中如何使用 styled-components
styled-components 的官方 Vue 版本目前已多年沒有更新,而且只支持到 Vue2,那么,在 Vue3 中怎么才能使用到 styled-components 呢,下面給大家介紹在 Vue3 中使用 styled-components的相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧2024-05-05
vant的picker組件設(shè)置文字超長(zhǎng)滾動(dòng)方式
這篇文章主要介紹了vant的picker組件設(shè)置文字超長(zhǎng)滾動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
vue?element-ui的el-input-number默認(rèn)值設(shè)置為空方法
這篇文章主要給大家介紹了關(guān)于vue?element-ui的el-input-number默認(rèn)值設(shè)置為空的相關(guān)資料,el-input-number組件是Element?UI非常常用的一個(gè)數(shù)字輸入框組件,它提供了默認(rèn)值設(shè)置的選項(xiàng),需要的朋友可以參考下2023-08-08

