Tips for using routing in uniapp
1. Overview
In uniapp development, routing is a very important aspect, it can realize the routing between pages Jump and pass parameters. This article will introduce the usage skills of routing in uniapp and give specific code examples.
2. Basic use of uniapp routing
In uniapp, the basic use of routing can be performed through APIs such as uni.navigateTo, uni.redirectTo, uni.reLaunch, uni.switchTab, etc. to perform page jumps. The usage scenarios of these APIs are slightly different, and the specific usage depends on the project requirements.
-
uni.navigateTo: used to open a new page and keep the current page. Suitable for ordinary page jumps.
Sample code:uni.navigateTo({ url: '/pages/detail/detail?id=1' });
-
uni.redirectTo: used to close the current page and open a new page. Suitable for page jumps that do not require returning to the previous page.
Sample code:uni.redirectTo({ url: '/pages/home/home' });
-
uni.reLaunch: Close all pages and open to a page within the application. It is suitable for scenarios where you scan the QR code to enter the mini program from other platforms.
Sample code:uni.reLaunch({ url: '/pages/login/login' });
-
uni.switchTab: Jump to the tarBar page and close all other non-tarBar pages. Suitable for jumping between pages in the bottom navigation bar.
Sample code:uni.switchTab({ url: '/pages/home/home' });
3. Transfer of uniapp routing parameters
In uniapp, data can be transferred between pages through URL parameters.
-
Passing parameters between pages
When page A jumps to page B, data can be passed through URL parameters. In the jump code of page A, the parameters are passed by splicing url:uni.navigateTo({ url: '/pages/detail/detail?id=' + id });
In page B, the parameter value can be obtained through uni.$route.query:
onLoad() { console.log(this.$route.query.id); }
-
Pass parameters when the page returns
In uniapp, you can return to the previous page through the uni.navigateBack method and pass parameters by calling the onBack method of the previous page. The specific code is as follows:
In page A, when jumping to page B, pass parameters and register the onBack method of the previous page:uni.navigateTo({ url: '/pages/detail/detail?id=' + id + '&callback=onBack' });
In page B, Get the parameter value, and call the onBack method of the previous page when the page returns to pass the parameters:
methods: { goBack() { uni.navigateBack({ delta: 1, success: () => { uni.getOpenerEventChannel().emit(this.asr_notify); } }); } }
In page A, register the onBack method and receive the parameters:
methods: { onBack(data) { console.log(data); } }
4. uniapp routing Interception and permission control
During the development process, sometimes it is necessary to control permissions on certain pages to prevent non-logged-in users from accessing certain pages.
In uniapp, routing interception and permission control can be implemented through navigation guards. The specific code is as follows:
-
Create a global route interceptor, in the main.js file:
// 全局路由拦截器 router.beforeEach((to, from, next) => { const token = uni.getStorageSync('token'); if (to.meta.requiresAuth && !token) { // 判断是否需要登录才能查看页面 next('/pages/login/login'); } else { next(); } });
-
Configure on the page that requires permission control Routing meta information:
export default { meta: { requiresAuth: true // 需要登录才能访问 } // 省略其他代码... }
Through the above operations, you can implement permission control on pages that require login to access. Users who are not logged in will be intercepted and jumped to the login page.
Summary:
This article introduces the basic usage of routing in uniapp, parameter passing methods, routing interception and permission control. Through reasonable use of routing, jumps and data transfer between pages can be achieved, improving the user experience of the application.
I hope this article will be helpful to your use of uniapp routing.
The above is the detailed content of Tips for using routing in uniapp. For more information, please follow other related articles on the PHP Chinese website!

The article discusses debugging strategies for mobile and web platforms, highlighting tools like Android Studio, Xcode, and Chrome DevTools, and techniques for consistent results across OS and performance optimization.

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

The article discusses end-to-end testing for UniApp applications across multiple platforms. It covers defining test scenarios, choosing tools like Appium and Cypress, setting up environments, writing and running tests, analyzing results, and integrat

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 common performance anti-patterns in UniApp development, such as excessive global data use and inefficient data binding, and offers strategies to identify and mitigate these issues for better app performance.

The article discusses using profiling tools to identify and resolve performance bottlenecks in UniApp, focusing on setup, data analysis, and optimization.

The article discusses strategies for optimizing network requests in UniApp, focusing on reducing latency, implementing caching, and using monitoring tools to enhance application performance.

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


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft