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 listData
named , which contains name
and age
two properties:
let listData = [
{name: 'Tom', age: 20},
{name: 'Jerry', age: 18},
{name: 'Lucy', age: 22},
]
2.2 In the page sending data, use uni.navigateTo
the method to open the target page, and url
add 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 onLoad
the 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.listData
is 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.vue
in
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.push
such as to pass.
If you are interested, you can pay attention to the official account biglead to get more content, or visit my course