当前位置:Gxlcms > JavaScript > 在vue中实现刷新和tab切换

在vue中实现刷新和tab切换

时间:2021-07-01 10:21:17 帮助过:14人阅读

下面我就为大家分享一篇vue刷新和tab切换实例,具有很好的参考价值,希望对大家有所帮助。

首先写个子组件用来放刷新的内容,命名为pull(可以看例子最后面放的。)

然后再要刷新的页面引用

  1. <template>
  2. <p class="fbjbox container">
  3. <p class="fbjbox1">
  4. <tab ref='tab' :list="['推荐','热门','最新']" width="33%" @change='tabchange' style="float:left"/>
  5. <span class="price" width="33%" @tap="change" >价格<span class="mui-icon mui-icon-arrowright"></span></span> //点击价格会排序
  6. </p>
  7. <p class="fbjbox2">
  8. <pull up="true" down="true" @up="next" @down="shuaxin" ref="pull">
  9. <p class="mui-row mui-col-xs-12 mui-clearfix">
  10. <span class="mui-col-xs-6" v-href="{name:'商品详情'}" rel="external nofollow" v-for="item in list">
  11. <p class="img"><img v-bind:src="item.goods_image"/></p>
  12. <h4>{{item.goods_name}}</h4>
  13. <p class="red1">¥{{item.goods_price}}</p>
  14. </span>
  15. </p>
  16. </pull>
  17. </p>
  18. </p>
  19. </template>
  20. <style>
  21. /*选项卡的颜色*/
  22. .tab{background:#fff;}
  23. .tab a.active{color:#D83737; border-bottom: 2px solid #D83737;}
  24. .red1{color:red;font-size:15px;padding-left:10px}
  25. h4{font-size:16px;font-weight:normal;color:#656565;padding-left:10px}
  26. </style>
  27. <style scoped>
  28. .container{font-family: "微软雅黑";}
  29. .mui-row{ overflow: hidden;margin-bottom: 20px;}
  30. .mui-row .mui-col-xs-6{width:49%;background-color:#fff;overflow: hidden;margin-top:6px;text-align: left;}
  31. .mui-row .mui-col-xs-6:nth-child(even){margin-left:2%;}
  32. .mui-row img{width:100%;}
  33. .mui-row .img{height:170px;}
  34. </style>
  35. <script>
  36. import tab from '../tab'
  37. import pull from '../public/pull'
  38. export default {
  39. created() {
  40. this.height = window.innerWidth * 40 / 64;
  41. this.is_zc = this.$route.query.is_zc == 1;
  42. this.uid = localStorage.getItem('uid');
  43. },
  44. mounted(){
  45. this.shuaxin(() => { });
  46. },
  47. data() {
  48. return {
  49. tab:0,
  50. list:[],
  51. order:1 //order是排序。1的默认的,2从低到高,3从高到低
  52. }
  53. },
  54. components: {tab,pull},
  55. computed:{ //cmd也是一个变量 ,而且根据选项卡的不同而不同
  56. cmd:function(){
  57. switch(this.tab){
  58. case 0: return "Mp/goodsTuijian";
  59. case 1: return "Mp/hotGoods";
  60. case 2: return "Mp/newGoods";
  61. }
  62. }
  63. },
  64. methods: {
  65. tabchange({index, data}) {
  66. this.tab = index;
  67. },
  68. shuaxin(done) { //首先进去以后的初始
  69. this.$api(this.cmd, { page: 0,order:this.sort }).then(list => { //除了把页面传过去还需要把排序的方式传过去
  70. this.list=list;
  71. done();
  72. //跳到一个新的选项卡时,页面从新从第一个.不是拉到的那个位置
  73. if(this.$refs.pull){
  74. this.$refs.pull.reset();
  75. this.$refs.pull.nodata = false;
  76. }
  77. });
  78. },
  79. next(done) {
  80. var page = this.list.length;
  81. this.$api(this.cmd, { page,order:this.sort }).then(list => {
  82. if (list.length == 0) { return done&&done(true);} //这个里面如果只出现done(true)的话可能就是只显示没有数据了,不会出现正在加载中
  83. this.list = this.list.concat(list);
  84. done();
  85. });
  86. },
  87. change(){ //点击价格时变化的class
  88. if(this.sort==1){this.sort=2;}
  89. else
  90. if(this.sort==2){this.sort=3;}
  91. else
  92. if(this.sort==3){this.sort=1;}
  93. this.shuaxin(() => { });
  94. }
  95. },
  96. watch:{ //监听。当选项卡改变的时候,刷新页面。
  97. tab:function(){
  98. this.shuaxin(() => { });
  99. },
  100. }
  101. }
  102. </script>

下面是命名为pull的子组件。

  1. <template>
  2. <p class="mui-scroll-wrapper" ref="box" v-bind:style="{top:top+'px'}">
  3. <p class="mui-pull-top-pocket mui-block" v-bind:class="{'mui-visibility':obj.y>0}" v-if="down">
  4. <p class="mui-pull" v-show="flag">
  5. <p class="mui-pull-loading mui-icon" v-bind:class="{'mui-spinner': type==2,'mui-icon-pulldown':type!=2}" v-bind:style="css1"></p>
  6. <p class="mui-pull-caption" v-if="type==0">下拉可以刷新</p>
  7. <p class="mui-pull-caption" v-if="type==1">释放立即刷新</p>
  8. <p class="mui-pull-caption" v-if="type==2">正在刷新</p>
  9. </p>
  10. </p>
  11. <p class="mui-scroll" @scrollstart.self="scrollstart" @scroll.self="scroll" @scrollbottom="scrollbottom">
  12. <slot>
  13. <p class="no-content">
  14. <i></i>
  15. <h4>暂无内容</h4>
  16. </p>
  17. </slot>
  18. <p class="mui-pull-bottom-pocket mui-block mui-visibility" v-if="type==4">
  19. <p class="mui-pull">
  20. <p class="mui-icon mui-spinner mui-visibility" style="transition: -webkit-transform 0.3s ease-in; transform: rotate(180deg); animation: spinner-spin 1s step-end infinite;position: relative;top: 8px;"></p>
  21. <p class="mui-pull-caption mui-visibility">正在加载...</p>
  22. </p>
  23. </p>
  24. <!--<p v-if="nodata" class="nodata">已经没有更多数据</p>-->
  25. <p v-if="nodata" class="yqxtsdkn"></p>
  26. </p>
  27. </p>
  28. </template>
  29. <style scoped>
  30. .mui-scroll-wrapper { position:relative;height:100%;}
  31. .nodata { color:#efefef;text-align:center;margin-top:10px;line-height: 30px; font-size: 12px; background:#0B2E4C}
  32. </style>
  33. <script>
  34. export default {
  35. mounted() {
  36. var box = this.$refs.box;
  37. this.obj = mui(box).scroll();
  38. },
  39. props: ["down", "up", "top"],
  40. data() {
  41. return {
  42. flag: false,
  43. Y: 0,
  44. obj: {},
  45. type: 0,
  46. nodata: false
  47. }
  48. },
  49. computed: {
  50. css1() {
  51. return {
  52. transition: this.type > 0 ? '-webkit-transform 0.3s ease-in' : "",
  53. transform: this.type > 0 ? 'rotate(180deg)' : "",
  54. animation: this.type == 2 ? "spinner-spin 1s step-end infinite" : ""
  55. };
  56. }
  57. },
  58. watch: {
  59. type(a, b) {
  60. if (b == 1) {
  61. this.type = 2;
  62. }
  63. if (a == 2) {
  64. this.flag = false;
  65. this.obj.setTranslate(0, 50);
  66. this.$emit("down", () => {
  67. this.type = 0;
  68. this.obj.setTranslate(0, 0);
  69. this.obj.reLayout();
  70. this.nodata = false;
  71. });
  72. }
  73. }
  74. },
  75. methods: {
  76. reset(){
  77. this.obj.setTranslate(0, 0);
  78. },
  79. scrollstart() {
  80. if (this.obj.lastY <= 0) {
  81. this.flag = true;
  82. } else {
  83. this.Y = 0;
  84. this.flag = false;
  85. }
  86. },
  87. scroll() {
  88. if (this.down && this.flag) {
  89. this.type = this.obj.y > 50 ? 1 : 0;
  90. if (this.obj.y > 0) {
  91. this.Y = this.obj.y;
  92. }
  93. }
  94. },
  95. scrollbottom() {
  96. if (this.nodata || !this.up || this.type == 4) return;
  97. this.type = 4;
  98. this.$emit("up", (n) => {
  99. this.type = 0;
  100. if (n == true) this.nodata = true;
  101. this.obj.reLayout();
  102. });
  103. }
  104. }
  105. }
  106. </script>

tab的一个例子

  1. <template>
  2. <p class="mypage">
  3. <p class="fbjbox">
  4. <p class="fbjbox1">
  5. <tab ref="tab" :list="['一级合伙人'+count,'二级合伙人']" width="50%" @change="tabchange" />
  6. </p>
  7. <p class="fbjbox2">
  8. <template v-if="list!=null&&tab==0">
  9. <h4 >一级合伙人总数{{count}}人</h4>
  10. <ul class="mui-table-view clear">
  11. <li class="mui-table-view-cell mui-media" v-for="item in list">
  12. <img class="mui-media-object mui-pull-left circle" v-bind:src="item.head_url" />
  13. <p class="mui-media-body">
  14. {{item.vname}}
  15. <p class='mui-ellipsis'>{{item.identity}}</p>
  16. </p>
  17. </li>
  18. </ul>
  19. </template>
  20. <template v-if="list!=null&&tab==1">
  21. <h4 >二级合伙人总数{{count}}人</h4>
  22. <ul class="mui-table-view clear">
  23. <li class="mui-table-view-cell mui-media" v-for="item,index in list">
  24. <p class="mui-media-body" v-cloak>
  25. {{type(index)}}人数<p class="mui-pull-right">{{item}}</p>
  26. </p>
  27. </li>
  28. </ul>
  29. </template>
  30. <!--<template v-if="list==null">
  31. <p class=" mui-text-center" style="padding: 50px;">
  32. <span class="mui-spinner"></span>
  33. </p>
  34. </template>-->
  35. <template v-if="list==[]">
  36. <p>暂无下线</p>
  37. </template>
  38. </p>
  39. </p>
  40. </p>
  41. </template>
  42. <style scoped="">
  43. p{color:#807E7E}
  44. .circle{margin-top:0px}
  45. .mypage{height:100%;}
  46. .mui-table-view .mui-media-object { line-height: 45px; max-width: 45px; height: 45px; }
  47. </style>
  48. <script>
  49. import tab from "../public/tab"
  50. import pull from "../public/pull"
  51. export default {
  52. mounted() {
  53. this.changemes();
  54. },
  55. components: {tab,pull},
  56. data() {
  57. return {
  58. tab:0,
  59. count:0,
  60. list: []
  61. }
  62. },
  63. computed:{
  64. cmd:function(){
  65. switch(this.tab){
  66. case 0: return "Member/oneLevel";
  67. case 1: return "Member/twoLevel";
  68. }
  69. }
  70. },
  71. methods: {
  72. type(num){
  73. switch (~~num) {
  74. case 1: return "游客";
  75. case 2: return "用户";
  76. case 3: return "粉丝";
  77. case 4: return "美人";
  78. case 5: return "卖手";
  79. case 6: return "合伙人";
  80. case 7: return "加盟商";
  81. }
  82. return "未知";
  83. },
  84. tabchange({index, data}) {
  85. this.tab = index;
  86. },
  87. changemes(){
  88. this.list=null;
  89. this.$api(this.cmd).then(list => {
  90. this.count=list.count;
  91. this.list=list.list;
  92. });
  93. }
  94. },
  95. watch:{
  96. tab:function(){
  97. this.changemes();
  98. }
  99. }
  100. }
  101. </script>

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

在vue中如何动态绑定表单元素的属性

在Vue中使用Vue.set动态新增对象属性方法

在vue2.0中没有dev-server.js下配置方法

以上就是在vue中实现刷新和tab切换的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行