uni-app two page pass value


highlight: a11y-dark

1 You can use routing to pass parameters to open the page and pass data.

As a list of data passed here, the following are the specific steps:

1.1 Determine the data format

For example, we want to pass a List listDatanamed , which contains nameand agetwo properties:

let listData = [
  {name: 'Tom', age: 20},
  {name: 'Jerry', age: 18},
  {name: 'Lucy', age: 22},
]
2.2 In the page sending data, use uni.navigateTothe method to open the target page, and urladd parameters after :
uni.navigateTo({
  url: '/pages/targetPage/targetPage?listData=' + JSON.stringify(listData),
})

Note that since routes can only pass string types, you need to use JSON.stringify()to convert List data to strings.

1.3 On the page receiving data, get the parameters through onLoadthe method , and use JSON.parse()to convert the string to the List data format:
onLoad: function(options) {
  let listData = JSON.parse(options.listData)
  console.log(listData)
}

Here options.listDatais the parameter passed in the sending page, JSON.parse()which is converted to the List data format by the method and printed out.

Through the above steps, we can open the page in uniapp and pass the List data.


2 Values ​​can be passed through global variables in uni-app.

The following are the specific steps:

2.1 Define global variables App.vuein

For example:

export default {
  globalData: {
    message: ''
  },
  ...
}
2.2 In the page that needs to pass the value, set the value of the global variable:
this.$root.globalData.message = 'Hello World!';
2.3 In the page that needs to get the passed value, directly access the value of the global variable:
console.log(this.$root.globalData.message); // 输出:Hello World!

In addition, you can also use the method of passing parameters when routing jumps, and specify parameters in methods $router.pushsuch as to pass.

If you are interested, you can pay attention to the official account  biglead  to get more content, or visit my course

Guess you like

Origin blog.csdn.net/zl18603543572/article/details/130458800