基于Vue實(shí)現(xiàn)tab欄切換內(nèi)容不斷實(shí)時(shí)刷新數(shù)據(jù)功能

先說(shuō)一下產(chǎn)品需求,就是有幾個(gè)tab欄,每個(gè)tab欄對(duì)應(yīng)的ajax請(qǐng)求不一樣,內(nèi)容區(qū)域一樣,內(nèi)容為實(shí)時(shí)刷新數(shù)據(jù),每3s需要重新請(qǐng)求,返回的數(shù)據(jù)在內(nèi)容區(qū)域展示,每點(diǎn)擊一次tab欄需停止其他tab欄ajax請(qǐng)求,防止阻塞,首次加載頁(yè)面的時(shí)候又不能5個(gè)ajax同時(shí)請(qǐng)求,只需要請(qǐng)求第一個(gè)就好

創(chuàng)新互聯(lián)公司提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì),成都品牌網(wǎng)站建設(shè),廣告投放平臺(tái)等致力于企業(yè)網(wǎng)站建設(shè)與公司網(wǎng)站制作,10余年的網(wǎng)站開發(fā)和建站經(jīng)驗(yàn),助力企業(yè)信息化建設(shè),成功案例突破上千家,是您實(shí)現(xiàn)網(wǎng)站建設(shè)的好選擇.

也沒有必要建立5個(gè)區(qū)域,控制顯示隱藏,浪費(fèi)性能,業(yè)務(wù)代碼就不貼了,把大概原理的代碼貼上來(lái)

基于Vue實(shí)現(xiàn)tab欄切換內(nèi)容不斷實(shí)時(shí)刷新數(shù)據(jù)功能

先是用jq實(shí)現(xiàn)了一版

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 <meta name="renderer" content="webkit">
 <title>Title</title>
 <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>
<div>
 <ul>
 <li>click</li>
 <li>click</li>
 <li>click</li>
 <li>click</li>
 <li>click</li>
 </ul>
</div>
<script>
 var arr=[
 function(){console.log(0);},
 function(){console.log(1);},
 function(){console.log(2);},
 function(){console.log(3);},
 function(){console.log(4);}
 ];
 var seti=setInterval(arr[0],1000)
 $('li').click(function(){
 clearInterval(seti)
 seti=setInterval(arr[$(this).index()],1000)
 })
</script>
</body>
</html>

再看vue版

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 <meta name="renderer" content="webkit">
 <title>Title</title>
 <script src="https://unpkg.com/vue@2.2.6/dist/vue.min.js"></script>
</head>
<body>
<div id="vm">
 <button @click="tab(0)">click0</button>
 <button @click="tab(1)">click1</button>
 <button @click="tab(2)">click2</button>
 <button @click="tab(3)">click3</button>
 <button @click="tab(4)">click4</button>
 <div>
 <p>{{show}}</p>
 </div>
</div>
<script>
 var vm1 = new Vue({
 el: '#vm',
 data: {
  arr:[
  function(){console.log(0);},
  function(){console.log(1);},
  function(){console.log(2);},
  function(){console.log(3);},
  function(){console.log(4);}
  ],
  time1:'',
  time2:'',
  show:'',
  num:0,
 },
 methods: {
  tab: function(index){
  //如果每個(gè)tab的方法不一樣,提前用一個(gè)數(shù)組把方法保存起來(lái)
  clearInterval(this.time1)
  this.time1=setInterval(this.arr[index],1000)
  //以下是調(diào)用同一種方法的時(shí)候可以不需要設(shè)置數(shù)組
  this.num = 0
  clearInterval(this.time2)
  this.time2 = this.fun(index)
  },
  fun:function(index){
  var _this=this;
  return setInterval(function(){
   //寫個(gè)隨機(jī)數(shù)顯示在頁(yè)面,具體業(yè)務(wù)需求應(yīng)該是ajax請(qǐng)求
   var random=String(parseInt(Math.random()*100000000000))
   //字符一個(gè)一個(gè)顯示在頁(yè)面上
   _this.show=index+random.substring(0,_this.num++);
  },300)
  }
 },
 mounted:function(){
  this.time1=setInterval(this.arr[0],1000)
 }
 });
</script>
</body>
</html>

以上所述是小編給大家介紹的基于Vue實(shí)現(xiàn)tab欄切換內(nèi)容不斷實(shí)時(shí)刷新數(shù)據(jù)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

網(wǎng)站名稱:基于Vue實(shí)現(xiàn)tab欄切換內(nèi)容不斷實(shí)時(shí)刷新數(shù)據(jù)功能
標(biāo)題路徑:http://muchs.cn/article28/pisecp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管網(wǎng)站排名、關(guān)鍵詞優(yōu)化、網(wǎng)站內(nèi)鏈、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作