Table of Contents
1. Introduction
2. Use the uni-swiper component
2.1 Create a tab page component
2.2 Create a tab bar component
3. Summary
Home Web Front-end uni-app How to implement tab switching function in uniapp

How to implement tab switching function in uniapp

Jul 04, 2023 pm 01:06 PM
uniapp switch Bookmark page

How to implement tab switching function in uniapp

1. Introduction

In mobile application development, tab switching is one of the common and important functions. As a cross-platform development framework, Uniapp can develop applications running on multiple platforms at the same time. This article will introduce how to implement the tab switching function in Uniapp and provide some sample code for reference.

2. Use the uni-swiper component

Uniapp provides the uni-swiper component, which can easily implement the tab switching function. The uni-swiper component is a carousel component that can set the sliding switching effect, which is very suitable for tab switching.

2.1 Create a tab page component

First, create a tab page component to implement specific page content. For example, we create three tabs, each tab corresponding to different page content.

<template>
  <view>
    <!-- 第一个标签页 -->
    <view v-if="currentTab === 0">
      <!-- 页面内容 -->
    </view>

    <!-- 第二个标签页 -->
    <view v-if="currentTab === 1">
      <!-- 页面内容 -->
    </view>

    <!-- 第三个标签页 -->
    <view v-if="currentTab === 2">
      <!-- 页面内容 -->
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      currentTab: 0  // 当前选中的标签页
    }
  }
}
</script>

<style>
/* 样式 */
</style>
Copy after login

In the above example, we control the currently selected tab through a currentTab variable. According to the value of currentTab, the corresponding tab content is displayed.

2.2 Create a tab bar component

Next, we create a tab bar component to switch tabs. We can use the uni-swiper component to implement the tab bar, bind a click event to each tab, and switch the value of currentTab when clicked.

<template>
  <view>
    <!-- 标签页切换 -->
    <uni-swiper :current="currentTab" @change="handleTabChange">
      <!-- 第一个标签 -->
      <uni-swiper-item>
        <view @click="currentTab = 0">标签页1</view>
      </uni-swiper-item>

      <!-- 第二个标签 -->
      <uni-swiper-item>
        <view @click="currentTab = 1">标签页2</view>
      </uni-swiper-item>

      <!-- 第三个标签 -->
      <uni-swiper-item>
        <view @click="currentTab = 2">标签页3</view>
      </uni-swiper-item>
    </uni-swiper>

    <!-- 标签页内容 -->
    <view>
      <tab-content :current-tab="currentTab"></tab-content>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      currentTab: 0  // 当前选中的标签页
    }
  },
  methods: {
    // 标签页切换事件
    handleTabChange(e) {
      this.currentTab = e.detail.current
    }
  },
  components: {
    'tab-content': TabContent
  }
}
</script>

<style>
/* 样式 */
</style>
Copy after login

In the above example, we use the uni-swiper component to wrap three uni-swiper-item, each uni-swiper-item represents a label. Switch the value of currentTab by clicking on the label, and then switch the tab page.

At the same time, we nest the tab page component in the tab bar component and pass the value of currentTab through the current-tab attribute to display the currently selected tab page. Content.

So far, we have completed the implementation of the tab switching function. You can customize the style and tab content according to actual needs.

3. Summary

This article introduces how to implement the tab switching function in Uniapp by using the uni-swiper component combined with the currentTab variable Control, realizing tab switching. I hope this article can be helpful to you, thank you for reading.

The above is the detailed content of How to implement tab switching function in uniapp. 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)

How to switch between 4g and 5g on Xiaomi Mi 14Ultra? How to switch between 4g and 5g on Xiaomi Mi 14Ultra? Feb 23, 2024 am 11:49 AM

Xiaomi 14Ultra is one of the most popular Xiaomi models this year. Xiaomi 14Ultra not only upgrades the processor and various configurations, but also brings many new functional applications to users. This can be seen from the sales of Xiaomi 14Ultra mobile phones. It is very popular, but there are some commonly used functions that you may not know yet. So how does Xiaomi 14Ultra switch between 4g and 5g? Let me introduce the specific content to you below! How to switch between 4g and 5g on Xiaomi 14Ultra? 1. Open the settings menu of your phone. 2. Find and select the "Network" and "Mobile Network" options in the settings menu. 3. In the mobile network settings, you will see the "Preferred network type" option. 4. Click or select this option and you will see

Operation tutorial for switching from win11 home version to professional version_Operation tutorial for switching from win11 home version to professional version Operation tutorial for switching from win11 home version to professional version_Operation tutorial for switching from win11 home version to professional version Mar 20, 2024 pm 01:58 PM

How to convert Win11 Home Edition to Win11 Professional Edition? In Win11 system, it is divided into Home Edition, Professional Edition, Enterprise Edition, etc., and most Win11 notebooks are pre-installed with Win11 Home Edition system. Today, the editor will show you the steps to switch from win11 home version to professional version! 1. First, right-click on this computer on the win11 desktop and properties. 2. Click Change Product Key or Upgrade Windows. 3. Then click Change Product Key after entering. 4. Enter the activation key: 8G7XN-V7YWC-W8RPC-V73KB-YWRDB and select Next. 5. Then it will prompt success, so you can upgrade win11 home version to win11 professional version.

How to start preview of uniapp project developed by webstorm How to start preview of uniapp project developed by webstorm Apr 08, 2024 pm 06:42 PM

Steps to launch UniApp project preview in WebStorm: Install UniApp Development Tools plugin Connect to device settings WebSocket launch preview

Which one is better, uniapp or mui? Which one is better, uniapp or mui? Apr 06, 2024 am 05:18 AM

Generally speaking, uni-app is better when complex native functions are needed; MUI is better when simple or highly customized interfaces are needed. In addition, uni-app has: 1. Vue.js/JavaScript support; 2. Rich native components/API; 3. Good ecosystem. The disadvantages are: 1. Performance issues; 2. Difficulty in customizing the interface. MUI has: 1. Material Design support; 2. High flexibility; 3. Extensive component/theme library. The disadvantages are: 1. CSS dependency; 2. Does not provide native components; 3. Small ecosystem.

How to implement dual system switching in Win10 system How to implement dual system switching in Win10 system Jan 03, 2024 pm 05:41 PM

Many friends may not be used to the win system when they first come into contact with it. There are dual systems in the computer. At this time, you can actually switch between the two systems. Let's take a look at the detailed steps for switching between the two systems. How to switch between two systems in win10 system 1. Shortcut key switching 1. Press the "win" + "R" keys to open Run 2. Enter "msconfig" in the run box and click "OK" 3. In the open "System Configuration" In the interface, select the system you need and click "Set as Default". After completion, "Restart" can complete the switch. Method 2. Select switch when booting 1. When you have dual systems, a selection operation interface will appear when booting. You can use the keyboard " Up and down keys to select the system

Switch the dual system boot mode of Apple computer Switch the dual system boot mode of Apple computer Feb 19, 2024 pm 06:50 PM

How to switch between Apple dual systems when starting up Apple computers are powerful devices. In addition to their own macOS operating system, you can also choose to install other operating systems, such as Windows, to achieve dual system switching. So how do we switch between the two systems when booting? This article will introduce to you how to switch between dual systems on Apple computers. First of all, before installing dual systems, we need to confirm whether our Apple computer supports dual system switching. Generally speaking, Apple computers are based on

What development tools do uniapp use? What development tools do uniapp use? Apr 06, 2024 am 04:27 AM

UniApp uses HBuilder

What basics are needed to learn uniapp? What basics are needed to learn uniapp? Apr 06, 2024 am 04:45 AM

uniapp development requires the following foundations: front-end technology (HTML, CSS, JavaScript) mobile development knowledge (iOS and Android platforms) Node.js other foundations (version control tools, IDE, mobile development simulator or real machine debugging experience)

See all articles