Home  >  Article  >  Web Front-end  >  Tips for using routing in uniapp

Tips for using routing in uniapp

WBOY
WBOYOriginal
2023-12-18 13:47:26941browse

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'
    });
  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'
    });
  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'
    });
  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'
    });

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
    });

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

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

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:

  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();
     }
    });
  2. 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!

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