亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Web Front-end uni-app Tips for using routing in uniapp

Tips for using routing in uniapp

Dec 18, 2023 pm 01:47 PM
uniapp routing Skill

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) { // 判斷是否需要登錄才能查看頁(yè)面
         next('/pages/login/login');
     } else {
         next();
     }
    });
  2. Configure on the page that requires permission control Routing meta information:

    export default {
     meta: {
         requiresAuth: true // 需要登錄才能訪問(wèn)
     }
     // 省略其他代碼...
    }

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 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

PHP Tutorial
1488
72
What are the disadvantages of uniapp What are the disadvantages of uniapp Apr 06, 2024 am 04:06 AM

UniApp has many conveniences as a cross-platform development framework, but its shortcomings are also obvious: performance is limited by the hybrid development mode, resulting in poor opening speed, page rendering, and interactive response. The ecosystem is imperfect and there are few components and libraries in specific fields, which limits creativity and the realization of complex functions. Compatibility issues on different platforms are prone to style differences and inconsistent API support. The security mechanism of WebView is different from native applications, which may reduce application security. Application releases and updates that support multiple platforms at the same time require multiple compilations and packages, increasing development and maintenance costs.

What is the difference between uniapp and flutter What is the difference between uniapp and flutter Apr 06, 2024 am 04:30 AM

UniApp is based on Vue.js, and Flutter is based on Dart. Both support cross-platform development. UniApp provides rich components and easy development, but its performance is limited by WebView; Flutter uses a native rendering engine, which has excellent performance but is more difficult to develop. UniApp has an active Chinese community, and Flutter has a large and global community. UniApp is suitable for scenarios with rapid development and low performance requirements; Flutter is suitable for complex applications with high customization and high performance.

How to start preview of uniapp project developed by webstorm How to start preview of uniapp project developed by webstorm Apr 08, 2024 pm 06:42 PM

Steps to launch UniApp project preview in WebStorm: Install UniApp Development Tools plugin Connect to device settings WebSocket launch preview

Win11 Tips Sharing: Skip Microsoft Account Login with One Trick Win11 Tips Sharing: Skip Microsoft Account Login with One Trick Mar 27, 2024 pm 02:57 PM

Win11 Tips Sharing: One trick to skip Microsoft account login Windows 11 is the latest operating system launched by Microsoft, with a new design style and many practical functions. However, for some users, having to log in to their Microsoft account every time they boot up the system can be a bit annoying. If you are one of them, you might as well try the following tips, which will allow you to skip logging in with a Microsoft account and enter the desktop interface directly. First, we need to create a local account in the system to log in instead of a Microsoft account. The advantage of doing this is

Which one is better, uniapp or mui? Which one is better, uniapp or mui? Apr 06, 2024 am 05:18 AM

Generally speaking, uni-app is better when complex native functions are needed; MUI is better when simple or highly customized interfaces are needed. In addition, uni-app has: 1. Vue.js/JavaScript support; 2. Rich native components/API; 3. Good ecosystem. The disadvantages are: 1. Performance issues; 2. Difficulty in customizing the interface. MUI has: 1. Material Design support; 2. High flexibility; 3. Extensive component/theme library. The disadvantages are: 1. CSS dependency; 2. Does not provide native components; 3. Small ecosystem.

A must-have for veterans: Tips and precautions for * and & in C language A must-have for veterans: Tips and precautions for * and & in C language Apr 04, 2024 am 08:21 AM

In C language, it represents a pointer, which stores the address of other variables; & represents the address operator, which returns the memory address of a variable. Tips for using pointers include defining pointers, dereferencing pointers, and ensuring that pointers point to valid addresses; tips for using address operators & include obtaining variable addresses, and returning the address of the first element of the array when obtaining the address of an array element. A practical example demonstrating the use of pointer and address operators to reverse a string.

Which is better, uniapp or native development? Which is better, uniapp or native development? Apr 06, 2024 am 05:06 AM

When choosing between UniApp and native development, you should consider development cost, performance, user experience, and flexibility. The advantages of UniApp are cross-platform development, rapid iteration, easy learning and built-in plug-ins, while native development is superior in performance, stability, native experience and scalability. Weigh the pros and cons based on specific project needs. UniApp is suitable for beginners, and native development is suitable for complex applications that pursue high performance and seamless experience.

What development tools do uniapp use? What development tools do uniapp use? Apr 06, 2024 am 04:27 AM

UniApp uses HBuilder

See all articles