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

How uniapp implements passing data between pages

王林
Release: 2023-10-21 09:27:39
Original
1614 people have browsed it

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.

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


<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

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

  1. 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'
Copy after login

},
mutations: {

updateData(state, payload) {
  state.data = payload
}
Copy after login

},
actions: {} ,
getters: {}
})

// Page A

<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

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!