vue實(shí)現(xiàn)發(fā)表評(píng)論功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)發(fā)表評(píng)論的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html>
<html>
?? ?<head>
?? ??? ?<meta charset="utf-8">
?? ??? ?<link rel="stylesheet" href="bootstrap-3.3.7.css" />
?? ??? ?<title></title>
?? ?</head>
?? ?<script src="vue-2.4.0.js"></script>
?? ?<body>
?? ?<div id="app">
?? ??? ?<ctm @func="show"></ctm>
?? ??? ?<ul class="list-group">
?? ??? ??? ?<li class="list-group-item" v-for="item in list" :key="item.id">
?? ??? ??? ??? ?<span class="badge">評(píng)論人{(lán){item.user}}</span>
?? ??? ??? ??? ?{{item.content}}
?? ??? ??? ?</li>
?? ??? ?</ul>
?? ? </div>
?? ? <template id="tmp1">
?? ??? ? <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" class="btn-primary" value="發(fā)表評(píng)論" @click="postComment"/>
?? ??? ??? ? </div>
?? ??? ? </div>
?? ? </template>
?? ? <script>
?? ??? ??? ?var com={
?? ??? ??? ??? ?template:'#tmp1',
?? ??? ??? ??? ?data(){
?? ??? ??? ??? ??? ?return{
?? ??? ??? ??? ??? ??? ?user:'',
?? ??? ??? ??? ??? ??? ?content:''
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?},
?? ??? ??? ??? ?methods:{
?? ??? ??? ??? ??? ?postComment(){
?? ??? ??? ??? ??? ??? ?//得到的先擺出來(lái)
?? ??? ??? ??? ??? ??? ?var comments={id:Date.now(),user:this.user,content:this.content}
?? ??? ??? ??? ??? ??? ?//獲取之前列表所有的內(nèi)容,由字符串形式轉(zhuǎn)換成對(duì)象形式
?? ??? ??? ??? ??? ??? ?var list=JSON.parse(localStorage.getItem('cmt')||'[]')
?? ??? ??? ??? ??? ??? ?//往list列表里面追加
?? ??? ??? ??? ??? ??? ?list.unshift(comments)
?? ??? ??? ??? ??? ??? ?//然后存到localStorage里面去,轉(zhuǎn)化為字符串形式
?? ??? ??? ??? ??? ??? ?localStorage.setItem('cmt',JSON.stringify(list))
?? ??? ??? ??? ??? ??? ?this.user=this.content=''
?? ??? ??? ??? ??? ??? ?this.$emit('func')
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ? ?? ??? ? ?// 創(chuàng)建 Vue 實(shí)例,得到 ViewModel
?? ? ?? ??? ? ?var vm = new Vue({
?? ? ?? ??? ? ? ?el: '#app',
?? ? ?? ??? ? ? ?data: {
?? ? ?? ??? ??? ??? ?list:[
?? ??? ??? ??? ??? ??? ?{id:Date.now(),user:'小小',content:'天生我材必有用'},
?? ??? ??? ??? ??? ??? ?{id:Date.now(),user:'小小1',content:'天生我材'},
?? ??? ??? ??? ??? ?]?? ?
?? ? ?? ??? ??? ?},
?? ??? ??? ??? ?//使用生命周期函數(shù),created函數(shù)已經(jīng)把data和methods創(chuàng)建好了,必須用this
?? ??? ??? ??? ?//需要自動(dòng)刷新,所以要用到父組件為子組件傳遞方法
?? ??? ??? ??? ?created(){
?? ??? ??? ??? ??? ?this.show()
?? ??? ??? ??? ?},
?? ??? ??? ??? ?methods: {
?? ??? ??? ??? ??? ?show(){
?? ??? ??? ??? ??? ??? ?//顯示,先獲取localStorage的內(nèi)容,轉(zhuǎn)為對(duì)象
?? ??? ??? ??? ??? ??? ?var list=JSON.parse(localStorage.getItem('cmt')||'[]')
?? ??? ??? ??? ??? ??? ?this.list=list
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?},
?? ??? ??? ??? ?components:{
?? ??? ??? ??? ??? ?'ctm':com
?? ??? ??? ??? ?}
?? ??? ??? ??? ?})
?? ?</script>
?? ?</body>
</html>效果圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)文章評(píng)論和回復(fù)列表
- VUE+Java實(shí)現(xiàn)評(píng)論回復(fù)功能
- Vue組件實(shí)現(xiàn)評(píng)論區(qū)功能
- vue開(kāi)發(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項(xiàng)目依賴(lài)檢查depcheck問(wèn)題
這篇文章主要介紹了vue項(xiàng)目依賴(lài)檢查depcheck問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
vue打開(kāi)新窗口并實(shí)現(xiàn)傳參的圖文實(shí)例
這篇文章主要給大家介紹了關(guān)于vue打開(kāi)新窗口并實(shí)現(xiàn)傳參的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
vue中的get方法\post方法如何實(shí)現(xiàn)傳遞數(shù)組參數(shù)
這篇文章主要介紹了vue中的get方法\post方法如何實(shí)現(xiàn)傳遞數(shù)組參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue3中導(dǎo)入和使用圖標(biāo)庫(kù)Font Awesome的實(shí)現(xiàn)步驟
Font Awesome 是互聯(lián)網(wǎng)的圖標(biāo)庫(kù)和工具包,被數(shù)百萬(wàn)設(shè)計(jì)師、開(kāi)發(fā)人員和內(nèi)容創(chuàng)建者使用,Font Awesome的圖標(biāo)非常豐富,基本涵蓋你所需要的所有,本文給大家介紹了Vue3中導(dǎo)入和使用圖標(biāo)庫(kù)Font Awesome的具體步驟,需要的朋友可以參考下2025-01-01
Vue3之父子組件異步props數(shù)據(jù)的傳值方式
這篇文章主要介紹了Vue3之父子組件異步props數(shù)據(jù)的傳值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
基于better-scroll 實(shí)現(xiàn)歌詞聯(lián)動(dòng)功能的代碼
BetterScroll 是一款重點(diǎn)解決移動(dòng)端(已支持 PC)各種滾動(dòng)場(chǎng)景需求的插件,BetterScroll 是使用純 JavaScript 實(shí)現(xiàn)的,這意味著它是無(wú)依賴(lài)的。本文基于better-scroll 實(shí)現(xiàn)歌詞聯(lián)動(dòng)功能,感興趣的朋友跟隨小編一起看看吧2020-05-05
Vue-router跳轉(zhuǎn)和location.href的區(qū)別及說(shuō)明
Vue?Router是Vue.js官方的路由管理器,它允許我們通過(guò)定義路由來(lái)管理應(yīng)用程序的不同視圖和狀態(tài),Vue路由跳轉(zhuǎn)主要有以下幾種方式:<router-link>標(biāo)簽、this.$router.push方法、this.$router.replace方法和this.$router.go方法2025-01-01

