How to configure uniapp routing
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.
- 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" } ] } }
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.
- 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.navigateBack
wait.
- 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' });
- 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' });
- uni.reLaunch
Close all pages and open a certain page of the application.
uni.reLaunch({ url: '/pages/about/about' });
- 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' });
- uni.navigateBack
Close the current page and return to the previous page.
uni.navigateBack({ delta: 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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]

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

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

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

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.

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

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