Vue.js實(shí)現(xiàn)模擬微信朋友圈開發(fā)demo
我用Vue.js實(shí)現(xiàn)微信朋友圈的一些功能,實(shí)現(xiàn)展示朋友圈,評論,點(diǎn)贊。
先構(gòu)造一個vue的實(shí)例,對會更改的數(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>評論 </a> </div>
實(shí)現(xiàn)朋友圈點(diǎn)贊
當(dāng)like的值變?yōu)橘潟r,向userLike中push一個點(diǎn)贊的username,然后將like的值變?yōu)槿∠.?dāng)用戶點(diǎn)擊取消按鈕的時候,將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)評論功能
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)評論回復(fù)功能
給comment中添加reply的key。由于微信的回復(fù)是平鋪的所以只要顯示誰對誰的回復(fù)即可,不需要進(jìn)行評論的嵌套。所以HTML中使用v-if對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(''); }
對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的時候,不能直接獲取到JSON的值,因?yàn)榭赡艿认旅婧瘮?shù)加載完后JSON的值還未拿到。所以會出現(xiàn)data數(shù)據(jù)為undefined的情況。所以需要在JSON的回調(diào)函數(shù)中進(jìn)行函數(shù)調(diào)用。
初始化showComt時,需要用到ready,當(dāng)DOM加載完后再執(zhí)行。
收獲:
學(xué)會了使用v-for、v-if、v-show,v-on:click等vue的方法,vue的構(gòu)造器,jQuery的Ajax相關(guān)的方法。
項(xiàng)目下載地址:Webchat-friendfeed_jb51.rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue組件大全包括(UI組件,開發(fā)框架,服務(wù)端,輔助工具,應(yīng)用實(shí)例,Demo示例)
- vue動態(tài)改變背景圖片demo分享
- vue.js配合$.post從后臺獲取數(shù)據(jù)簡單demo分享
- Vue.js實(shí)現(xiàn)的表格增加刪除demo示例
- 用 Vue.js 遞歸組件實(shí)現(xiàn)可折疊的樹形菜單(demo)
- 用Go+Vue.js快速搭建一個Web應(yīng)用(初級demo)
- 基于vue的短信驗(yàn)證碼倒計(jì)時demo
- Vue仿手機(jī)qq的實(shí)例代碼(demo)
- Vue插件寫、用詳解(附demo)
- 詳解Vue demo實(shí)現(xiàn)商品列表的展示
相關(guān)文章
vue項(xiàng)目如何實(shí)現(xiàn)ip和localhost同時訪問
這篇文章主要介紹了vue項(xiàng)目如何實(shí)現(xiàn)ip和localhost同時訪問,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10vue項(xiàng)目使用.env文件配置全局環(huán)境變量的方法
這篇文章主要介紹了vue項(xiàng)目使用.env文件配置全局環(huán)境變量的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10一文詳解Pinia和Vuex與兩個Vue狀態(tài)管理模式
這篇文章主要介紹了一文詳解Pinia和Vuex與兩個Vue狀態(tài)管理模式,Pinia和Vuex一樣都是是vue的全局狀態(tài)管理器。其實(shí)Pinia就是Vuex5,只不過為了尊重原作者的貢獻(xiàn)就沿用了這個看起來很甜的名字Pinia2022-08-08vue使用window.open()跳轉(zhuǎn)頁面的代碼案例
這篇文章主要介紹了vue中對window.openner的使用,vue使用window.open()跳轉(zhuǎn)頁面的代碼案例,本文通過實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-11-11Vue實(shí)現(xiàn)驗(yàn)證碼登錄的方法實(shí)例
最近在自己寫頁面,然后寫登錄注冊UI的時候需要一個驗(yàn)證碼組件,去搜一下沒找到什么合適的,于是自己寫一個,這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)驗(yàn)證碼登錄的相關(guān)資料,需要的朋友可以參考下2022-03-03基于腳手架創(chuàng)建Vue項(xiàng)目實(shí)現(xiàn)步驟詳解
這篇文章主要介紹了基于腳手架創(chuàng)建Vue項(xiàng)目實(shí)現(xiàn)步驟詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08vue2.0+webpack環(huán)境的構(gòu)造過程
本文分步驟給大家介紹了vue2.0+webpack環(huán)境的構(gòu)造過程的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-11-11詳解vue express啟動數(shù)據(jù)服務(wù)
本篇文章主要介紹了vue express啟動數(shù)據(jù)服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07