源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Vue.js实现模拟微信朋友圈开发demo

  • 时间:2022-11-07 19:53 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Vue.js实现模拟微信朋友圈开发demo
我用Vue.js实现微信朋友圈的一些功能,实现展示朋友圈,评论,点赞。 先构造一个vue的实例,对会更改的数据进行双向绑定, 我用JSON伪造模版数据,先实现显示朋友圈的效果,使用v-for方法去循环ALLFeeds中的每一项item生成包括name、content、time在内的各项数据。 微信朋友圈实现效果 [img]http://files.jb51.net/file_images/article/201704/201704201009342.png?2017320101221[/img] 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>

[b]实现朋友圈点赞[/b] 当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 = '赞';
      }
     }
    }

[b]实现评论功能[/b] 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('');
    }
[b]实现评论回复功能[/b] 给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>
[b]遇到的坑: [/b] 当异步加载JSON的时候,不能直接获取到JSON的值,因为可能等下面函数加载完后JSON的值还未拿到。所以会出现data数据为undefined的情况。所以需要在JSON的回调函数中进行函数调用。 初始化showComt时,需要用到ready,当DOM加载完后再执行。 [b]收获: [/b] 学会了使用v-for、v-if、v-show,v-on:click等vue的方法,vue的构造器,jQuery的Ajax相关的方法。 [url=https://github.com/HanhanTalk/Webchat-friendfeed]Github链接[/url] 项目下载地址:[url=http://xiazai.jb51.net/201704/yuanma/Webchat-friendfeed_jb51.rar]Webchat-friendfeed_jb51.rar[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部