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.
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() {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>return { param: 'Hello Uniapp' }</pre><div class="contentsignin">Copy after login</div></div><p>},<br> methods: {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>navigateToPageB() { uni.navigateTo({ url: '/pages/pageB?pageParam=' + this.param }) }</pre><div class="contentsignin">Copy after login</div></div><p>}<br>}<br></script>
// Page B
<text>{{pageParam}}</text>
<script><br>export default {<br> data() {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>return { pageParam: '' }</pre><div class="contentsignin">Copy after login</div></div><p>},<br> onLoad(options) {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>this.pageParam = options.pageParam</pre><div class="contentsignin">Copy after login</div></div><p>}<br>}<br></script>
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.
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'</p><p>export default {<br> computed: {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>...mapState(['data'])</pre><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div><p>} ,<br> methods: {</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>navigateToPageB() { this.$store.commit('updateData', 'Hello Page B') uni.navigateTo({ url: '/pages/pageB' }) }</pre><div class="contentsignin">Copy after login</div></div><p>}<br>}<br></script>
// Page B
<text>{{data}}</text>