Vue.js實(shí)現(xiàn)模擬微信朋友圈開(kāi)發(fā)demo
我用Vue.js實(shí)現(xiàn)微信朋友圈的一些功能,實(shí)現(xiàn)展示朋友圈,評(píng)論,點(diǎn)贊。
先構(gòu)造一個(gè)vue的實(shí)例,對(duì)會(huì)更改的數(shù)據(jù)進(jìn)行雙向綁定,
我用JSON偽造模版數(shù)據(jù),先實(shí)現(xiàn)顯示朋友圈的效果,使用v-for方法去循環(huán)ALLFeeds中的每一項(xiàng)item生成包括name、content、time在內(nèi)的各項(xiàng)數(shù)據(jù)。
微信朋友圈實(shí)現(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>評(píng)論
</a>
</div>
實(shí)現(xiàn)朋友圈點(diǎn)贊
當(dāng)like的值變?yōu)橘潟r(shí),向userLike中push一個(gè)點(diǎn)贊的username,然后將like的值變?yōu)槿∠?。?dāng)用戶點(diǎn)擊取消按鈕的時(shí)候,將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 = '贊';
}
}
}
實(shí)現(xiàn)評(píng)論功能
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('');
}
實(shí)現(xiàn)評(píng)論回復(fù)功能
給comment中添加reply的key。由于微信的回復(fù)是平鋪的所以只要顯示誰(shuí)對(duì)誰(shuí)的回復(fù)即可,不需要進(jìn)行評(píng)論的嵌套。所以HTML中使用v-if對(duì)comment中是否存在reply進(jìn)行判斷。
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('');
}
對(duì)comment中是否存在reply進(jìn)行判斷
<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>回復(fù) <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>
遇到的坑:
當(dāng)異步加載JSON的時(shí)候,不能直接獲取到JSON的值,因?yàn)榭赡艿认旅婧瘮?shù)加載完后JSON的值還未拿到。所以會(huì)出現(xiàn)data數(shù)據(jù)為undefined的情況。所以需要在JSON的回調(diào)函數(shù)中進(jìn)行函數(shù)調(diào)用。
初始化showComt時(shí),需要用到ready,當(dāng)DOM加載完后再執(zhí)行。
收獲:
學(xué)會(huì)了使用v-for、v-if、v-show,v-on:click等vue的方法,vue的構(gòu)造器,jQuery的Ajax相關(guān)的方法。
項(xiàng)目下載地址:Webchat-friendfeed_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue組件大全包括(UI組件,開(kāi)發(fā)框架,服務(wù)端,輔助工具,應(yīng)用實(shí)例,Demo示例)
- vue動(dòng)態(tài)改變背景圖片demo分享
- vue.js配合$.post從后臺(tái)獲取數(shù)據(jù)簡(jiǎn)單demo分享
- Vue.js實(shí)現(xiàn)的表格增加刪除demo示例
- 用 Vue.js 遞歸組件實(shí)現(xiàn)可折疊的樹(shù)形菜單(demo)
- 用Go+Vue.js快速搭建一個(gè)Web應(yīng)用(初級(jí)demo)
- 基于vue的短信驗(yàn)證碼倒計(jì)時(shí)demo
- Vue仿手機(jī)qq的實(shí)例代碼(demo)
- Vue插件寫、用詳解(附demo)
- 詳解Vue demo實(shí)現(xiàn)商品列表的展示
相關(guān)文章
vuex state及mapState的基礎(chǔ)用法詳解
這篇文章主要介紹了vuex state及mapState的基礎(chǔ)用法詳解,本文通過(guò)實(shí)例代碼相結(jié)合的形式給大家介紹的非常詳細(xì),需要的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-04-04
vue3.0項(xiàng)目小白填坑之vue3.0+vite獲取環(huán)境變量
Vue3項(xiàng)目使用vite作為打包工具時(shí),環(huán)境變量可以保存在.env文件中,在build時(shí)進(jìn)行解析,這篇文章主要給大家介紹了關(guān)于vue3.0項(xiàng)目小白填坑之vue3.0+vite獲取環(huán)境變量的相關(guān)資料,需要的朋友可以參考下2024-03-03
vue預(yù)覽本地pdf文件方法之vue-pdf組件使用
這篇文章主要介紹了vue預(yù)覽本地pdf文件方法之vue-pdf組件使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue項(xiàng)目安裝less和less-loader的詳細(xì)步驟
這篇文章主要介紹了Vue項(xiàng)目安裝less和less-loader的詳細(xì)步驟,本文分步驟結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
vue2.x 父組件監(jiān)聽(tīng)子組件事件并傳回信息的方法
本篇文章主要介紹了vue2.x 父組件監(jiān)聽(tīng)子組件事件并傳回信息的方法,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07
vue3?element?plus?table?selection展示數(shù)據(jù),默認(rèn)選中功能方式
這篇文章主要介紹了vue3?element?plus?table?selection展示數(shù)據(jù),默認(rèn)選中功能方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
element日期選擇器el-date-picker樣式圖文詳解
最近寫的項(xiàng)目里面有一個(gè)功能是日期選擇功能,第一反應(yīng)是使用element里面的el-date-picker組件,下面這篇文章主要給大家介紹了關(guān)于element日期選擇器el-date-picker樣式的相關(guān)資料,需要的朋友可以參考下2022-09-09

