How uniapp implements passing data between pages
Uniapp implements how to transfer data between pages, requiring specific code examples
In uniapp development, transferring data between pages is a very common requirement. Through reasonable data transfer, we can achieve data sharing and interaction when page jumps. This article will introduce how to implement data transfer between pages in uniapp and give specific code examples.
- Use URL parameters to pass data
Pending data through URL parameters is the most common and simplest way. By adding parameters in the URL of the jump link, data can be transferred between pages. The following is a sample code:
// Page A
<text>{{param}}</text> <button @click="navigateToPageB">跳转至页面B</button>
<script><br>export default {<br> data() {</script>
return { param: 'Hello Uniapp' }
},
methods: {
navigateToPageB() { uni.navigateTo({ url: '/pages/pageB?pageParam=' + this.param }) }
}
}
// Page B
<text>{{pageParam}}</text>
<script><br>export default {<br> data() {</script>
return { pageParam: '' }
},
onLoad(options) {
this.pageParam = options.pageParam
}
}
In page A, we jump to page B through the button click event and carry the parameter pageParam
. In page B, the passed parameters are obtained through the onLoad
life cycle function and assigned to pageParam
, and then displayed on the page.
- Use Vuex global state management
If you need to share data between multiple pages, using Vuex is a good choice. Vuex is a state management pattern developed specifically for Vue.js applications and can also be used in uniapp. Here is a sample code:
// store/index.js
import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
data: 'Hello Uniapp'
},
mutations: {
updateData(state, payload) { state.data = payload }
},
actions: {} ,
getters: {}
})
// Page A
<text>{{data}}</text> <button @click="navigateToPageB">跳转至页面B</button>
<script><br>import { mapState } from 'vuex'</script>
export default {
computed: {
...mapState(['data'])
} ,
methods: {
navigateToPageB() { this.$store.commit('updateData', 'Hello Page B') uni.navigateTo({ url: '/pages/pageB' }) }
}
}
// Page B
<text>{{data}}</text>
<script><br>import { mapState } from 'vuex'</script>
export default {
computed: {
...mapState(['data'])
}
}
In this example, we use mapState
in page A The helper function maps data
in store
to the data
computed property of the current component. In the click event of page A, modify the data
data in store
through the commit
method, and then jump to the page. Page B also uses the mapState
auxiliary function to map data
in store
to the current component.
Summary:
The above two methods are common methods for uniapp to transfer data between pages. URL parameter transfer of data is simple and clear, suitable for situations where the amount of data is small; while using Vuex is suitable for situations where data needs to be shared among multiple pages. Choosing the appropriate method to transfer data between pages based on actual needs can improve development efficiency and user experience.
The above is the detailed content of How uniapp implements passing data between pages. 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

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

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
