Home  >  Article  >  Web Front-end  >  Detailed introduction to the Tab page component in uniapp

Detailed introduction to the Tab page component in uniapp

PHPz
PHPzOriginal
2023-04-17 14:15:153233browse

The Tab page component is a very commonly used component in both mobile application development and web application development. In actual development, the jump and return of Tab pages are issues that must be dealt with frequently. This article will give a detailed introduction to the Tab page component of the uniapp platform, mainly covering knowledge points such as how to jump and return to the Tab page.

1. How to use the Tab page component in uniapp

In uniapp, the Tab page component is a very practical component that can easily combine the effect of a Tab page. The basic idea of ​​using the Tab page component is to implement each tab page as a component, and then use the routing jump mechanism provided by uniapp to switch between different Tab pages.

First, we need to open the pages.json file of the uniapp project, and then add the following code:

{
  "path": "pages/tabbar",
  "style": {
    "navigationBarTitleText": "Tab页列表"
  },
  "tabBar": {
    "color": "#666",
    "selectedColor": "#4d4d4d",
    "backgroundColor": "#ffffff",
    "list": [{
      "pagePath": "pages/tabbar/home",
      "text": "首页",
      "iconPath": "static/tabbar/home.png",
      "selectedIconPath": "static/tabbar/home-active.png"
    }, {
      "pagePath": "pages/tabbar/message",
      "text": "消息",
      "iconPath": "static/tabbar/message.png",
      "selectedIconPath": "static/tabbar/message-active.png"
    }]
  }
},

The above example code defines a TabBar page, including two Tab pages, namely Home page and message page. In it, we need to implement each tab page as an independent page.

Next, we open the page manager of uniapp and create two pages, home and message, under the specified path. These two pages can be any page you want, but it should be noted that they must be consistent with the path pointed by the pagePath in the tabBarItem of the TabBar component.

<template>
  <view class="content">
    <text>这里是首页</text>
  </view>
</template>

<script>
export default {
  name: 'Home'
}
</script>
<template>
  <view class="content">
    <text>这里是消息页</text>
  </view>
</template>

<script>
export default {
  name: 'Message'
}
</script>

In the above code, we simply created two pages, home and message.

After completing the above steps, we can see that a Tab page will appear at the bottom of the application, which contains two tab pages, corresponding to the home and message pages just created.

2. How to jump to the Tab page

In uniapp, the Tab page jump is very similar to the ordinary page jump, and they are all implemented through the routing jump mechanism provided by uniapp. However, since you are jumping between Tab pages, you need to use specific instructions to control the target Tab page of the jump.

First, we need to add an instruction to the Tab page to specify the jump of the Tab page:

<template>
  <view class="content">
    <button @click="gotoMessage">跳转到消息页</button>
  </view>
</template>

<script>
export default {
  name: 'Home',
  methods: {
    gotoMessage: function () {
      uni.switchTab({
        url: '/pages/tabbar/message'
      })
    }
  }
}
</script>

In the above code, we implement Tab through the uni.switchTab() method Page jump. Among them, the incoming url parameter specifies the path to the target Tab page to jump to.

When we perform a jump operation on the Tab page, the application will automatically jump to the target Tab page.

3. How to return in the Tab page

One thing to note when performing the return operation in the Tab page is that performing the return operation in the Tab page will directly exit the application instead of returning. Previous page. Therefore, we need to add an additional instruction to the Tab page to control the return operation.

First, we need to add an instruction to a certain Tab page, and then implement the return operation through the uni.navigateBack() method:

<template>
  <view class="content">
    <button @click="backToHome">返回首页</button>
  </view>
</template>

<script>
export default {
  name: 'Message',
  methods: {
    backToHome: function () {
      uni.switchTab({
        url: '/pages/tabbar/home'
      })
    }
  }
}
</script>

In the above code, we also use The uni.switchTab() method is used to implement the jump operation of the Tab page. However, the difference from before is that we jump to a Tab page instead of an ordinary page.

It should be noted that we used the switchTab() method at this time. This is because using the navigateBack() method in the Tab page will directly exit the application and cannot perform the return operation like a normal page. Therefore, we need to convert the return operation into a jump between Tab pages.

Summary:

This article mainly introduces how to use the Tab page component to jump and return to the Tab page in the uniapp platform. In actual development, the Tab page component is a very practical component. It can easily combine Tab page effects that suit you, and achieve smoother page jumps and operations. Readers can use the Tab page component in their own applications based on the content described in this article to achieve the Tab page effect they need.

The above is the detailed content of Detailed introduction to the Tab page component in uniapp. For more information, please follow other related articles on the PHP Chinese website!

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