Home Web Front-end uni-app How to configure uniapp routing

How to configure uniapp routing

Apr 18, 2023 pm 02:09 PM

With the popularity and demand for mobile applications increasing, more and more developers are beginning to use cross-platform development technology to build applications. UniApp, as a cross-platform development framework, has gradually emerged under this trend and is welcomed by more and more developers. Routing is a very important component when developing applications with UniApp, which allows you to navigate between different pages.

In UniApp, routing is used to control page jumps and navigation of the application. If you are already familiar with the routing mechanism of Vue.js, you will feel very familiar when using UniApp's routing. UniApp's routing mechanism is well compatible with Vue.js routing and provides some additional functions and APIs.

For beginners, the application of UniApp routing may not be clear at first, so where should you fill in UniApp routing? In the next article, we will introduce the application of UniApp routing in detail and where to fill it out.

  1. Routing configuration file

In UniApp, you can configure routing in the routing configuration file. The routing configuration file is generally located in the pages.json file in the root directory. Its function is to configure the routing mapping table of the application. In the routing configuration file, you need to specify the path of each page, the page title, the page icon and other attributes. If you need to add the page to the navigation bar, you also need to specify the tabBar attribute. The following is a simple routing configuration file example:

{
  "pages": [
    {
      "path": "pages/index/index",
      "style": {
        "navigationBarTitleText": "首页",
        "navigationBarBackgroundColor": "#ffffff"
      }
    },
    {
      "path": "pages/about/about",
      "style": {
        "navigationBarTitleText": "关于我们",
        "navigationBarBackgroundColor": "#ffffff"
      },
      "tabBar": {
        "text": "关于",
        "iconPath": "../../static/images/tabbar/about.png",
        "selectedIconPath": "../../static/images/tabbar/about-active.png"
      }
    }
  ],
  "tabBar": {
    "color": "#a9b7b7",
    "selectedColor": "#35b4b4",
    "borderStyle": "black",
    "backgroundColor": "#ffffff",
    "list": [
      {
        "pagePath": "pages/index/index",
        "text": "首页",
        "iconPath": "../../static/images/tabbar/home.png",
        "selectedIconPath": "../../static/images/tabbar/home-active.png"
      },
      {
        "pagePath": "pages/about/about",
        "text": "关于",
        "iconPath": "../../static/images/tabbar/about.png",
        "selectedIconPath": "../../static/images/tabbar/about-active.png"
      }
    ]
  }
}
Copy after login

In the above code, we define two pages: index and about, and in A tabBar attribute is added to the about page to specify the position and style of the page in the navigation bar. At the same time, we also define the navigation bar style of the application. In this example, we use the index page as the home page of the navigation bar, so set it as the default page in the tabBar attribute.

  1. Use UniApp Routing API

In addition to routing configuration in the routing configuration file, UniApp also provides a complete set of routing APIs that can be flexibly used in business logic. use. These APIs include uni.navigateTo, uni.redirectTo, uni.reLaunch, uni.switchTab, and uni.navigateBackwait.

  • uni.navigateTo

Jump from the current page to a page in the application. If the target page has not been opened, the API will open a new page; if the target page is already open, the API will put the target page on the top of the stack.

uni.navigateTo({
  url: '/pages/about/about'
});
Copy after login
  • uni.redirectTo

Close the current page and jump to a certain page in the application. This API closes the current page, so there is no way to return to the current page via the back button.

uni.redirectTo({
  url: '/pages/about/about'
});
Copy after login
  • uni.reLaunch

Close all pages and open a certain page of the application.

uni.reLaunch({
  url: '/pages/about/about'
});
Copy after login
  • uni.switchTab

Jump to a tab page of the application. This API can only be used to jump to the application. The tab page.

uni.switchTab({
  url: '/pages/index/index'
});
Copy after login
  • uni.navigateBack

Close the current page and return to the previous page.

uni.navigateBack({
  delta: 1  // 返回的页面数,如果为空,则返回上一个页面
});
Copy after login
  1. Summary

In UniApp, routing is an important part of application jump and navigation. When developing applications, you can configure and use routing through routing configuration files and routing APIs. The routing configuration file can well control the position and style of the page, while the routing API provides a flexible programming method, allowing you to flexibly jump and navigate pages in business logic. Learning to use the UniApp routing mechanism can bring great convenience and efficiency to your development.

The above is the detailed content of How to configure uniapp routing. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 do I use uni-app's social sharing APIs? How do I use uni-app's social sharing APIs? Mar 13, 2025 pm 06:30 PM

The article details how to integrate social sharing into uni-app projects using uni.share API, covering setup, configuration, and testing across platforms like WeChat and Weibo.

How do I use preprocessors (Sass, Less) with uni-app? How do I use preprocessors (Sass, Less) with uni-app? Mar 18, 2025 pm 12:20 PM

Article discusses using Sass and Less preprocessors in uni-app, detailing setup, benefits, and dual usage. Main focus is on configuration and advantages.[159 characters]

How do I use uni-app's animation API? How do I use uni-app's animation API? Mar 18, 2025 pm 12:21 PM

The article explains how to use uni-app's animation API, detailing steps to create and apply animations, key functions, and methods to combine and control animation timing.Character count: 159

What are the different types of testing that you can perform in a UniApp application? What are the different types of testing that you can perform in a UniApp application? Mar 27, 2025 pm 04:59 PM

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

How can you reduce the size of your UniApp application package? How can you reduce the size of your UniApp application package? Mar 27, 2025 pm 04:45 PM

The article discusses strategies to reduce UniApp package size, focusing on code optimization, resource management, and techniques like code splitting and lazy loading.

How do I use uni-app's storage API (uni.setStorage, uni.getStorage)? How do I use uni-app's storage API (uni.setStorage, uni.getStorage)? Mar 18, 2025 pm 12:22 PM

The article explains how to use uni-app's storage APIs (uni.setStorage, uni.getStorage) for local data management, discusses best practices, troubleshooting, and highlights limitations and considerations for effective use.

What is the file structure of a uni-app project? What is the file structure of a uni-app project? Mar 14, 2025 pm 06:55 PM

The article details the file structure of a uni-app project, explaining key directories like common, components, pages, static, and uniCloud, and crucial files such as App.vue, main.js, manifest.json, pages.json, and uni.scss. It discusses how this o

How can you optimize images for web performance in UniApp? How can you optimize images for web performance in UniApp? Mar 27, 2025 pm 04:50 PM

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.

See all articles