国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Vue.js實現模擬微信朋友圈開發demo

2019-11-19 16:47:01
字體:
來源:轉載
供稿:網友

我用Vue.js實現微信朋友圈的一些功能,實現展示朋友圈,評論,點贊。

先構造一個vue的實例,對會更改的數據進行雙向綁定,

我用JSON偽造模版數據,先實現顯示朋友圈的效果,使用v-for方法去循環ALLFeeds中的每一項item生成包括name、content、time在內的各項數據。

微信朋友圈實現效果

HTML代碼:

 <div class="border" v-for="item in AllFeeds" track-by="$index">      <div class="user-pic">       <img v-bind:src="item.url" alt="">      </div>      <div class="user-content">       <h2 class="spacing">{{item.name}}</h2>       <p class="spacing">{{item.content}}</p>       <img class="spacing" v-bind:src="item.picUrl" alt="">       <span class="spacing time">{{item.time}}</span>       <div class="commit" v-show="item.showComt">        <a v-on:click="likeClick($event,item)" class="btn btn-left" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >         <span class="icon-heart-empty"></span>{{item.like}}        </a>        <a v-on:click="comtClick($event,item)" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="btn btn-comment">         <span class="icon-comment-alt"></span>評論        </a>       </div>

實現朋友圈點贊

當like的值變為贊時,向userLike中push一個點贊的username,然后將like的值變為取消。當用戶點擊取消按鈕的時候,將userLike數組中添加的贊pop掉。

likeClick: function (event, feed) {     event.stopPropagation();     if (feed.like === "贊") {      if (gUserInfo) {       feed.userLike.push(gUserInfo.username);       feed.like = '取消';      }     } else {      if (gUserInfo) {       feed.userLike.pop();       feed.like = '贊';      }     }    }

實現評論功能

input的val()push到content的值里。

 inputClick: function (event) {     event.stopPropagation();     var comText = $(".inset input").val();     this.clickFeed.comment.push({"name": gUserInfo.username, "content": comText});     $(".inset").addClass("hidden");     $(".overplay").addClass("hidden");     $('.inset input').val('');    }

實現評論回復功能

給comment中添加reply的key。由于微信的回復是平鋪的所以只要顯示誰對誰的回復即可,不需要進行評論的嵌套。所以HTML中使用v-if對comment中是否存在reply進行判斷。

 replyClick:function(event){     event.stopPropagation();     var replyText = $(".replybox input").val();     this.clickFeed.comment.push({      "name":gUserInfo.username,      "content":replyText,      "reply":this.replyUser     });     $(".replybox").addClass("hidden");     $(".overplay").addClass("hidden");     $(".replybox input").val('');    }

對comment中是否存在reply進行判斷 

<div v-if="!(onecommet.reply)">             <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" v-on:click="replyComt($event,item,onecommet)" class="reply">              <span class="user">{{onecommet.name}}:</span>              {{onecommet.content}}             </a>            </div>            <div v-else>             <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" v-on:click="replyComt($event,item,onecommet)" class="reply">              <span class="user">{{userinfo.username}}</span>回復 <span class="user">{{replyUser}}:              <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="reply">{{onecommet.content}}</a>             </span>             </a>            </div>

遇到的坑:

當異步加載JSON的時候,不能直接獲取到JSON的值,因為可能等下面函數加載完后JSON的值還未拿到。所以會出現data數據為undefined的情況。所以需要在JSON的回調函數中進行函數調用。

初始化showComt時,需要用到ready,當DOM加載完后再執行。

收獲:

學會了使用v-for、v-if、v-show,v-on:click等vue的方法,vue的構造器,jQuery的Ajax相關的方法。

Github鏈接

項目下載地址:Webchat-friendfeed_jb51.rar

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 老河口市| 大安市| 尉氏县| 永昌县| 民和| 峨边| 灵寿县| 秭归县| 营山县| 周口市| 寿光市| 康马县| 金沙县| 琼结县| 轮台县| 广平县| 若尔盖县| 林芝县| 姚安县| 大足县| 宁德市| 临猗县| 奎屯市| 新巴尔虎右旗| 太白县| 凉城县| 台东县| 岱山县| 巫山县| 铜梁县| 罗江县| 巴南区| 林芝县| 凌云县| 宁乡县| 建德市| 肃南| 连山| 浮梁县| 天柱县| 剑川县|