Home> Web Front-end> uni-app> body text

Tips for using routing in uniapp

WBOY
Release: 2023-12-18 13:47:26
Original
856 people have browsed it

Tips for using routing in uniapp

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.

  1. 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' });
    Copy after login
  2. 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' });
    Copy after login
  3. 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' });
    Copy after login
  4. 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' });
    Copy after login

3. Transfer of uniapp routing parameters
In uniapp, data can be transferred between pages through URL parameters.

  1. 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 });
    Copy after login

In page B, the parameter value can be obtained through uni.$route.query:

onLoad() { console.log(this.$route.query.id); }
Copy after login
  1. 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' });
    Copy after login

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); } }); } }
Copy after login

In page A, register the onBack method and receive the parameters:

methods: { onBack(data) { console.log(data); } }
Copy after login

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:

  1. 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(); } });
    Copy after login
  2. Configure on the page that requires permission control Routing meta information:

    export default { meta: { requiresAuth: true // 需要登录才能访问 } // 省略其他代码... }
    Copy after login

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!

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!