这篇文章主要介绍Vue.js怎么实现模拟微信朋友圈开发demo,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
Vue具体轻量级框架、简单易学、双向数据绑定、组件化、数据和结构的分离、虚拟DOM、运行速度快等优势,Vue中页面使用的是局部刷新,不用每次跳转页面都要请求所有数据和dom,可以大大提升访问速度和用户体验。
我用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" >
</div>
<div class="user-content">
<h3 class="spacing">{{item.name}}</h3>
<p class="spacing">{{item.content}}</p>
<img class="spacing" v-bind:src="item.picUrl" >
<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加载完后再执行。
以上是“Vue.js怎么实现模拟微信朋友圈开发demo”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。