在uniapp中,你可以使用全局变量、Vuex状态管理、或者使用uni.navigateTo
或uni.redirectTo
的url
参数来传递数据。
以下是使用uni.navigateTo
和URL参数传递数据的例子:
假设你有一个表单页面FormPage.vue
,需要将表单数据传递到DetailPage.vue
。
1.在FormPage.vue
中,当你准备跳转到DetailPage.vue
时,将表单数据通过URL传递
// FormPage.vue
methods: {
goToDetailPage() {
const formData = this.formData; // 假设这是你要传递的数据
const query = Object.keys(formData)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(formData[key])}`)
.join('&');
uni.navigateTo({
url: `/pages/DetailPage/DetailPage?${query}`
});
}
}
2.在DetailPage.vue
中,使用onLoad
生命周期钩子来接收传递的数据:
// DetailPage.vue
export default {
data() {
return {
formData: {}
};
},
onLoad(options) {
this.formData = options;
}
}
现在DetailPage.vue
的formData
属性将包含来自FormPage.vue
的数据。请注意,这种方法有大小限制,因为URL有长度限制。对于大量或复杂的数据,你可能需要考虑其他数据传递方法,如全局变量或Vuex状态管理。