vue組件實(shí)現(xiàn)發(fā)表評(píng)論功能
本文實(shí)例為大家分享了vue組件實(shí)現(xiàn)發(fā)表評(píng)論的具體代碼,供大家參考,具體內(nèi)容如下
今天看了vue相關(guān)的視頻,所以跟著做一個(gè)小demo把知識(shí)串聯(lián)起來,內(nèi)容很簡單但是也算是學(xué)習(xí)道路上的一點(diǎn)進(jìn)步。
1 思路分析
發(fā)表評(píng)論模塊寫入一個(gè)組件,提高復(fù)用性。
關(guān)鍵點(diǎn):
1)、子組件通過localStorage向父組件傳值
2)、子組件有自己的data存儲(chǔ)user和content,即評(píng)論人和評(píng)論內(nèi)容,也就是dom元素綁定的數(shù)據(jù)。
3)、點(diǎn)擊‘發(fā)表評(píng)論’后,首先是將各條評(píng)論存入localStorage,然后通過在組件出綁定的函數(shù)調(diào)用父組件中的loadComments()加載評(píng)論。
4)、vue生命周期的熟悉。在created()中寫入loadComments(),每次頁面加載就會(huì)載入評(píng)論數(shù)據(jù)。
2 源代碼
需要vue.js和bootstrap.js兩個(gè)文件
<!DOCTYPE html>
<html>
<head>
? ? <meta charset="utf-8" />
? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
? ? <title>Page Title</title>
? ? <meta name="viewport" content="width=device-width, initial-scale=1">
? ? <script src="../lib/vue.js"></script>
? ? <link rel="stylesheet" href="../lib/bootstrap.css">
? ? <style>
? ? li{
? ? ? ? list-style:none;
? ? }
? ? </style>
</head>
<body>
? ? <div id="app">
? ? ? ? <com @func="loadComments"></com>
? ? ? ? <ul class="list-group">
? ? ? ? ? ? <li class="list-group-item" v-for="item in list">
? ? ? ? ? ? ? ? {{item.content}}<span class="badge">{{item.user}}</span>
? ? ? ? ? ? </li>
? ? ? ? </ul>
? ? </div>
? ? ? ? <!-- 評(píng)論區(qū)組件 -->
? ? <template id="tmp">
? ? ? ? <div>
? ? ? ? ? ? <div class="form-group"><label>評(píng)論人</label><input class="form-control" id="user" v-model:value="user"/></div>
? ? ? ? ? ? ?<div class="form-group"><label>評(píng)論內(nèi)容</label><input class="form-control" id="content" v-model:value="content"/></div> ??
? ? ? ? ? ? ? ? <div><input type="button" class="btn btn-primary" value="發(fā)表評(píng)論" @click="postComments"/></div>
? ? ? ? </div>
? ? </template>
? ? <script>
? ? var tmp={
? ? ? ? template:"#tmp",
? ? ? ? data:function(){
? ? ? ? ? ? return {
? ? ? ? ? ? ? ? user:'',
? ? ? ? ? ? ? ? content:''
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? methods:{
? ? ? ? ? ? postComments(){
? ? ? ? ? ? ? ? var comment={user:this.user,content:this.content};
? ? ? ? ? ? ? ? var list=JSON.parse(localStorage.getItem('cmts')||'[]');
? ? ? ? ? ? ? ? list.unshift(comment);
? ? ? ? ? ? ? ? localStorage.setItem('cmts',JSON.stringify(list));//數(shù)組對(duì)象和字符串相互轉(zhuǎn)換的過程
? ? ? ? ? ? ? ? this.user='';
? ? ? ? ? ? ? ? this.content='';
? ? ? ? ? ? ? ? this.$emit('func');
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? var vm=new Vue({
? ? ? ? el:"#app",
? ? ? ? data:{
? ? ? ? ? ? list:[]
? ? ? ? },
? ? ? ? created(){
? ? ? ? ? ?this.loadComments();
? ? ? ? },
? ? ? ? methods:{
? ? ? ? ? ? loadComments(){
? ? ? ? ? ? ? ? this.list=JSON.parse(localStorage.getItem('cmts')||'[]');
? ? ? ? ? ? }
? ? ? ? },
? ? ? ? components:{
? ? ? ? ? ? 'com':tmp
? ? ? ? }
? ? ? ??
? ? });
? ? </script>
</body>
</html>以上就是本文的全部內(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組件實(shí)現(xiàn)評(píng)論區(qū)功能
- vue開發(fā)實(shí)現(xiàn)評(píng)論列表
- vue實(shí)現(xiàn)評(píng)論列表
- Vue實(shí)現(xià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)論功能
相關(guān)文章
Vue3.0導(dǎo)出數(shù)據(jù)為自定義樣式Excel的詳細(xì)實(shí)例
在許多的后臺(tái)系統(tǒng)中少不了導(dǎo)出Excel表格的功能,下面這篇文章主要給大家介紹了關(guān)于Vue3.0導(dǎo)出數(shù)據(jù)為自定義樣式Excel的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
Vue實(shí)現(xiàn)當(dāng)前頁面刷新的五種方法總結(jié)
這篇文章主要介紹了Vue中實(shí)現(xiàn)頁面刷新的5種方法,包括使用$router.go(0)、location.reload()、通過router-view的key屬性、使用v-if指令手動(dòng)觸發(fā)組件重新渲染,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02
使用vue實(shí)現(xiàn)滑動(dòng)滾動(dòng)條來加載數(shù)據(jù)
在vuejs中,我們經(jīng)常使用axios來請(qǐng)求數(shù)據(jù),但是有時(shí)候,我們請(qǐng)求的數(shù)據(jù)量很大,那么我們?nèi)绾螌?shí)現(xiàn)滑動(dòng)滾動(dòng)條來加載數(shù)據(jù)呢,接下來小編就給大家介紹一下在vuejs中如何實(shí)現(xiàn)滑動(dòng)滾動(dòng)條來動(dòng)態(tài)加載數(shù)據(jù),需要的朋友可以參考下2023-10-10
vue前端實(shí)現(xiàn)導(dǎo)出頁面為pdf(分頁導(dǎo)出、不分頁導(dǎo)出及分模塊導(dǎo)出)
在實(shí)際應(yīng)用中可能用戶希望將系統(tǒng)中一個(gè)頁面展示的所有數(shù)據(jù)報(bào)表,用PDF的文件格式下載下來,以便于其他用途,這篇文章主要給大家介紹了關(guān)于vue前端實(shí)現(xiàn)導(dǎo)出頁面為pdf(分頁導(dǎo)出、不分頁導(dǎo)出及分模塊導(dǎo)出)的相關(guān)資料,需要的朋友可以參考下2024-06-06
解決vue腳手架項(xiàng)目打包后路由視圖不顯示的問題
今天小編就為大家分享一篇解決vue腳手架項(xiàng)目打包后路由視圖不顯示的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue中數(shù)據(jù)不響應(yīng)的問題及解決
這篇文章主要介紹了vue中數(shù)據(jù)不響應(yīng)的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue中watch與watchEffect的區(qū)別詳細(xì)解讀
這篇文章主要介紹了Vue中watch與watchEffect的區(qū)別詳細(xì)解讀,watch函數(shù)與watchEffect函數(shù)都是監(jiān)聽器,在寫法和用法上有一定區(qū)別,是同一功能的兩種不同形態(tài),底層都是一樣的,需要的朋友可以參考下2023-11-11

