Home Web Front-end JS Tutorial Implement refresh and tab switching in vue

Implement refresh and tab switching in vue

Jun 06, 2018 am 10:27 AM

Below I will share with you an example of vue refresh and tab switching, which has a good reference value and I hope it will be helpful to everyone.

First write a subcomponent to hold the refreshed content, named pull (you can see it at the end of the example.)

Then reference the page to be refreshed

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

<template>

   <p class="fbjbox container">

   <p class="fbjbox1">

  <tab ref=&#39;tab&#39; :list="[&#39;推荐&#39;,&#39;热门&#39;,&#39;最新&#39;]" width="33%" @change=&#39;tabchange&#39; style="float:left"/>

 <span class="price" width="33%" @tap="change" >价格<span class="mui-icon mui-icon-arrowright"></span></span> //点击价格会排序

   </p>

  <p class="fbjbox2">

    

   <pull up="true" down="true" @up="next" @down="shuaxin" ref="pull">

    

    <p class="mui-row mui-col-xs-12 mui-clearfix">

      <span class="mui-col-xs-6" v-href="{name:&#39;商品详情&#39;}" rel="external nofollow" v-for="item in list">

       <p class="img"><img v-bind:src="item.goods_image"/></p>

       <h4>{{item.goods_name}}</h4>

       <p class="red1">¥{{item.goods_price}}</p>

      </span>

     </p>

    

   </pull>

   

  </p>

</p>

</template>

<style>

 /*选项卡的颜色*/

 .tab{background:#fff;}

 .tab a.active{color:#D83737; border-bottom: 2px solid #D83737;}

 .red1{color:red;font-size:15px;padding-left:10px}

 h4{font-size:16px;font-weight:normal;color:#656565;padding-left:10px}

</style>

<style scoped>

 .container{font-family: "微软雅黑";}

 .mui-row{ overflow: hidden;margin-bottom: 20px;}

 .mui-row .mui-col-xs-6{width:49%;background-color:#fff;overflow: hidden;margin-top:6px;text-align: left;}

 .mui-row .mui-col-xs-6:nth-child(even){margin-left:2%;}

 .mui-row img{width:100%;}

 .mui-row .img{height:170px;}

</style>

<script>

import tab from &#39;../tab&#39;

import pull from &#39;../public/pull&#39;

 export default {

   created() {

   this.height = window.innerWidth * 40 / 64;

   this.is_zc = this.$route.query.is_zc == 1;

   this.uid = localStorage.getItem(&#39;uid&#39;);

    

   

  },

  mounted(){

    this.shuaxin(() => { });

   

  },

  data() {

    return {

    tab:0,

    list:[],

   order:1 //order是排序。1的默认的,2从低到高,3从高到低

   }

  },

  components: {tab,pull},

  computed:{  //cmd也是一个变量 ,而且根据选项卡的不同而不同

   cmd:function(){

    switch(this.tab){

     case 0: return "Mp/goodsTuijian";

     case 1: return "Mp/hotGoods";

     case 2: return "Mp/newGoods";

    }

     

   }

  },

  methods: {

   tabchange({index, data}) {

    this.tab = index;

    },

  shuaxin(done) { //首先进去以后的初始

    this.$api(this.cmd, { page: 0,order:this.sort }).then(list => { //除了把页面传过去还需要把排序的方式传过去

      this.list=list;

     done();

 //跳到一个新的选项卡时,页面从新从第一个.不是拉到的那个位置

   if(this.$refs.pull){

      this.$refs.pull.reset();

      this.$refs.pull.nodata = false;

      }

    });

   },

  next(done) {

    var page = this.list.length;

    this.$api(this.cmd, { page,order:this.sort }).then(list => {

     if (list.length == 0) { return done&&done(true);}  //这个里面如果只出现done(true)的话可能就是只显示没有数据了,不会出现正在加载中

     this.list = this.list.concat(list);

     done();

    });

   },

  change(){ //点击价格时变化的class

    if(this.sort==1){this.sort=2;}

    else

    if(this.sort==2){this.sort=3;}

    else

    if(this.sort==3){this.sort=1;}

    this.shuaxin(() => { });

   }

    

  },

  watch:{   //监听。当选项卡改变的时候,刷新页面。

   tab:function(){

    this.shuaxin(() => { });

     

   },

    

  }

 }

</script>

Copy after login

The following is the subcomponent named pull.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

<template>

 <p class="mui-scroll-wrapper" ref="box" v-bind:style="{top:top+&#39;px&#39;}">

  <p class="mui-pull-top-pocket mui-block" v-bind:class="{&#39;mui-visibility&#39;:obj.y>0}" v-if="down">

   <p class="mui-pull" v-show="flag">

    <p class="mui-pull-loading mui-icon" v-bind:class="{&#39;mui-spinner&#39;: type==2,&#39;mui-icon-pulldown&#39;:type!=2}" v-bind:style="css1"></p>

    <p class="mui-pull-caption" v-if="type==0">下拉可以刷新</p>

    <p class="mui-pull-caption" v-if="type==1">释放立即刷新</p>

    <p class="mui-pull-caption" v-if="type==2">正在刷新</p>

   </p>

  </p>

  <p class="mui-scroll" @scrollstart.self="scrollstart" @scroll.self="scroll" @scrollbottom="scrollbottom">

   <slot>

    <p class="no-content">

     <i></i>

     <h4>暂无内容</h4>

    </p>

   </slot>

   <p class="mui-pull-bottom-pocket mui-block mui-visibility" v-if="type==4">

    <p class="mui-pull">

     <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>

<p class="mui-pull-caption mui-visibility">正在加载...</p>

</p>

</p>

<!--<p v-if="nodata" class="nodata">已经没有更多数据</p>-->

<p v-if="nodata" class="yqxtsdkn"></p>

</p>

</p>

</template>

<style scoped>

 .mui-scroll-wrapper { position:relative;height:100%;}

 .nodata { color:#efefef;text-align:center;margin-top:10px;line-height: 30px; font-size: 12px; background:#0B2E4C}

</style>

<script>

 export default {

  mounted() {

   var box = this.$refs.box;

   this.obj = mui(box).scroll();

  },

  props: ["down", "up", "top"],

  data() {

   return {

    flag: false,

    Y: 0,

    obj: {},

    type: 0,

    nodata: false

   }

  },

  computed: {

   css1() {

    return {

     transition: this.type > 0 ? &#39;-webkit-transform 0.3s ease-in&#39; : "",

     transform: this.type > 0 ? &#39;rotate(180deg)&#39; : "",

     animation: this.type == 2 ? "spinner-spin 1s step-end infinite" : ""

    };

   }

  },

  watch: {

   type(a, b) {

    if (b == 1) {

     this.type = 2;

    }

    if (a == 2) {

     this.flag = false;

     this.obj.setTranslate(0, 50);

     this.$emit("down", () => {

      this.type = 0;

      this.obj.setTranslate(0, 0);

      this.obj.reLayout();

      this.nodata = false;

     });

    }

   }

  },

  methods: {

  reset(){

    this.obj.setTranslate(0, 0);

   },

   scrollstart() {

    if (this.obj.lastY <= 0) {

     this.flag = true;

    } else {

     this.Y = 0;

     this.flag = false;

    }

   },

   scroll() {

    if (this.down && this.flag) {

     this.type = this.obj.y > 50 ? 1 : 0;

     if (this.obj.y > 0) {

      this.Y = this.obj.y;

     }

    }

   },

   scrollbottom() {

    if (this.nodata || !this.up || this.type == 4) return;

    this.type = 4;

    this.$emit("up", (n) => {

     this.type = 0;

     if (n == true) this.nodata = true;

     this.obj.reLayout();

    });

   }

  }

 }

</script>

Copy after login

An example of tab

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

<template>

 <p class="mypage">

  

   <p class="fbjbox">

    <p class="fbjbox1">

    <tab ref="tab" :list="[&#39;一级合伙人&#39;+count,&#39;二级合伙人&#39;]" width="50%" @change="tabchange" />

   </p>

   <p class="fbjbox2">

    <template v-if="list!=null&&tab==0">

      <h4 >一级合伙人总数{{count}}人</h4>

      <ul class="mui-table-view clear">

       <li class="mui-table-view-cell mui-media" v-for="item in list">

        <img class="mui-media-object mui-pull-left circle" v-bind:src="item.head_url" />

         <p class="mui-media-body">

          {{item.vname}}

          <p class=&#39;mui-ellipsis&#39;>{{item.identity}}</p>

         </p>

       </li>

        

     </ul>

      

    </template>

    <template v-if="list!=null&&tab==1">    

     <h4 >二级合伙人总数{{count}}人</h4>

      <ul class="mui-table-view clear">

       <li class="mui-table-view-cell mui-media" v-for="item,index in list">

         <p class="mui-media-body" v-cloak>

          {{type(index)}}人数<p class="mui-pull-right">{{item}}</p>

         </p>

       </li>

        

      </ul>

    </template>

    <!--<template v-if="list==null">

     <p class=" mui-text-center" style="padding: 50px;">

       

     <span class="mui-spinner"></span>

     </p>

    </template>-->

    <template v-if="list==[]">

     <p>暂无下线</p>

    </template>

   </p>

  </p>

  

 </p>

</template>

<style scoped="">

p{color:#807E7E}

 .circle{margin-top:0px}

 .mypage{height:100%;}

 .mui-table-view .mui-media-object { line-height: 45px; max-width: 45px; height: 45px; }

  

</style>

<script>

import tab from "../public/tab"

import pull from "../public/pull"

 export default {

  mounted() {

   this.changemes();

  },

  components: {tab,pull},

  data() {

   return {

     tab:0,

     count:0,

     list: []

   }

  },

   computed:{  

   cmd:function(){

    switch(this.tab){

     case 0: return "Member/oneLevel";

     case 1: return "Member/twoLevel";

    }

     

   }

  },

   methods: {

    type(num){  

    switch (~~num) {

     case 1: return "游客";

     case 2: return "用户";

     case 3: return "粉丝";

     case 4: return "美人";

     case 5: return "卖手";

     case 6: return "合伙人";

     case 7: return "加盟商";

    }

     return "未知";

    },

   tabchange({index, data}) {

    this.tab = index;

    },

   changemes(){

    this.list=null;

     this.$api(this.cmd).then(list => {

      this.count=list.count;

      this.list=list.list;

      

    });

   }

  },

  watch:{

   tab:function(){

    this.changemes();

   }

  }

 }

</script>

Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. help.

Related articles:

How to dynamically bind the attributes of form elements in vue

Use Vue.set dynamically new in Vue Method of adding object attributes

There is no configuration method under dev-server.js in vue2.0

The above is the detailed content of Implement refresh and tab switching in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript Engines: Comparing Implementations JavaScript Engines: Comparing Implementations Apr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript: Exploring the Versatility of a Web Language JavaScript: Exploring the Versatility of a Web Language Apr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration) How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration) Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

From C/C   to JavaScript: How It All Works From C/C to JavaScript: How It All Works Apr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration) Building a Multi-Tenant SaaS Application with Next.js (Backend Integration) Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

See all articles