Vue.js實現(xiàn)模擬微信朋友圈開發(fā)demo
我用Vue.js實現(xiàn)微信朋友圈的一些功能,實現(xiàn)展示朋友圈,評論,點贊。
先構(gòu)造一個vue的實例,對會更改的數(shù)據(jù)進行雙向綁定,
我用JSON偽造模版數(shù)據(jù),先實現(xiàn)顯示朋友圈的效果,使用v-for方法去循環(huán)ALLFeeds中的每一項item生成包括name、content、time在內(nèi)的各項數(shù)據(jù)。
微信朋友圈實現(xiàn)效果

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>
實現(xiàn)朋友圈點贊
當like的值變?yōu)橘潟r,向userLike中push一個點贊的username,然后將like的值變?yōu)槿∠.斢脩酎c擊取消按鈕的時候,將userLike數(shù)組中添加的贊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 = '贊';
}
}
}
實現(xiàn)評論功能
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('');
}
實現(xiàn)評論回復功能
給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的值,因為可能等下面函數(shù)加載完后JSON的值還未拿到。所以會出現(xiàn)data數(shù)據(jù)為undefined的情況。所以需要在JSON的回調(diào)函數(shù)中進行函數(shù)調(diào)用。
初始化showComt時,需要用到ready,當DOM加載完后再執(zhí)行。
收獲:
學會了使用v-for、v-if、v-show,v-on:click等vue的方法,vue的構(gòu)造器,jQuery的Ajax相關的方法。
項目下載地址:Webchat-friendfeed_jb51.rar
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue項目如何實現(xiàn)ip和localhost同時訪問
這篇文章主要介紹了vue項目如何實現(xiàn)ip和localhost同時訪問,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
vue項目使用.env文件配置全局環(huán)境變量的方法
這篇文章主要介紹了vue項目使用.env文件配置全局環(huán)境變量的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
一文詳解Pinia和Vuex與兩個Vue狀態(tài)管理模式
這篇文章主要介紹了一文詳解Pinia和Vuex與兩個Vue狀態(tài)管理模式,Pinia和Vuex一樣都是是vue的全局狀態(tài)管理器。其實Pinia就是Vuex5,只不過為了尊重原作者的貢獻就沿用了這個看起來很甜的名字Pinia2022-08-08
vue使用window.open()跳轉(zhuǎn)頁面的代碼案例
這篇文章主要介紹了vue中對window.openner的使用,vue使用window.open()跳轉(zhuǎn)頁面的代碼案例,本文通過實例代碼給大家詳細講解,需要的朋友可以參考下2022-11-11
基于腳手架創(chuàng)建Vue項目實現(xiàn)步驟詳解
這篇文章主要介紹了基于腳手架創(chuàng)建Vue項目實現(xiàn)步驟詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08
vue2.0+webpack環(huán)境的構(gòu)造過程
本文分步驟給大家介紹了vue2.0+webpack環(huán)境的構(gòu)造過程的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-11-11

