How to implement parameter passing and return between pages in uniapp
1. Parameter passing
In uniapp, we can pass parameters through the path Parameters, props parameters and vuex parameters are used to transfer parameters between pages.
- Path passing parameters
Path passing parameters refers to splicing parameters directly after the URL and passing them when jumping to another page. When jumping, we pass the parameters to the next page by adding parameters after the URL. On the next page, the value of the parameters can be obtained through the uni.getStorageSync() method.
// 页面A uni.navigateTo({ url: '/pages/B/B?param1=123¶m2=456' }) // 页面B onLoad: function (options) { console.log(options.param1) //输出123 console.log(options.param2) //输出456 }
- props parameter passing
props parameter passing is to pass the parameters as attributes of the subcomponent. When using the subcomponent in the parent component, additional attributes are added to pass the parameters. Get the passed parameters through this.$props in the child component.
// 父组件 <template> <child-component :param1="param1"></child-component> </template> <script> export default { data() { return { param1: '123', param2: '456' } } } </script> // 子组件 <template> <view>{{ $props.param1 }}</view> <view>{{ $props.param2 }}</view> </template>
- vuex parameter passing
vuex is a state management tool in uniapp. We can use vuex to transfer parameters between pages. When sending parameters, store the parameters in the state of vuex. When receiving parameters, obtain the value of the parameter through the getters method of vuex.
// 页面A <template> <button @click="sendParam">传递参数</button> </template> <script> export default { methods: { sendParam() { this.$store.commit('SET_PARAM', '参数值') uni.navigateTo({ url: '/pages/B/B' }) } } } </script> // 页面B onLoad: function () { console.log(this.$store.getters.param) //输出参数值 } // store import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { param: '' }, mutations: { SET_PARAM(state, value) { state.param = value } }, getters: { param: state => state.param } }) export default store
2. Postback
In uniapp, postback between pages can be achieved through the uni.navigateBack method and the EventBus event bus.
- uni.navigateBackbackback
The uni.navigateBack method is used to return to the previous page, and can be passed back by passing parameters.
// 页面A uni.navigateTo({ url: '/pages/B/B' }) // 页面B uni.navigateBack({ delta: 1, success: function () { uni.getOpenerEventChannel().emit('acceptDataFromB', {data: '回传的参数'}) } }) // 页面A onLoad: function () { const eventChannel = this.getOpenerEventChannel() eventChannel.on('acceptDataFromB', function (data) { console.log(data) //输出{data: '回传的参数'} }) }
- EventBus Event Bus Postback
EventBus is a tool for communication between components. In uniapp, you can use uni.$emit to publish events and uni. $on subscribes to the event and returns it.
// 页面A // main.js Vue.prototype.$eventBus = new Vue() // 页面B this.$eventBus.$emit('acceptDataFromB', {data: '回传的参数'}) uni.navigateBack({ delta: 1 }) // 页面A this.$eventBus.$on('acceptDataFromB', function (data) { console.log(data) //输出{data: '回传的参数'} })
Through the above method, we can implement parameter transfer and return between pages in uniapp. Through path parameter passing, props parameter passing, vuex parameter passing, uni.navigateBack passing back and EventBus passing back, etc., you can choose the appropriate method according to actual needs to realize the passing and returning of parameters.
The above is the detailed content of How to implement parameter passing and postback between pages in uniapp. For more information, please follow other related articles on the PHP Chinese website!

如何在uni-app中实现图片预览功能引言:在移动应用开发中,图片预览是一项常用的功能。在uni-app中,我们可以通过使用uni-ui插件或自定义组件来实现图片预览功能。本文将介绍如何在uni-app中实现图片预览功能,并附带代码示例。一、使用uni-ui插件实现图片预览功能uni-ui是由DCloud开发的一套基于Vue.js的组件库,提供了丰富的UI组

如何在uniapp中实现相机拍照功能现在的手机功能越来越强大,几乎每个手机都配备了高像素的相机。在UniApp中实现相机拍照功能,可以为你的应用程序增添更多的交互性和丰富性。本文将针对UniApp,介绍如何使用uni-app插件来实现相机拍照功能,并提供代码示例供参考。一、安装uni-app插件首先,我们需要安装一个uni-app的插件,该插件可以方便地在u

本篇文章给大家带来了关于uniapp跨域的相关知识,其中介绍了uniapp和小程序分包的相关问题,每个使用分包小程序必定含有一个主包。所谓的主包,即放置默认启动页面/TabBar 页面,以及一些所有分包都需用到公共资源/JS 脚本;而分包则是根据开发者的配置进行划分,希望对大家有帮助。

uniapp是一种基于Vue.js的跨平台开发框架,它可以同时开发微信小程序、App和H5页面。在uniapp中,我们可以通过使用uni-api来访问设备的各种功能,包括地理位置获取功能。本文将介绍在uniapp中如何使用地理位置获取功能,并附上代码示例。首先,在uniapp中使用地理位置获取功能,我们需要在manifest.json文件中申请权限。在man

uniapp中如何使用视频播放器组件随着移动互联网的发展,视频已成为人们日常生活中不可或缺的娱乐方式之一。在uniapp中,我们可以通过使用视频播放器组件来实现视频的播放和控制。本文将介绍如何在uniapp中使用视频播放器组件,并提供相应的代码示例。一、引入视频播放器组件在uniapp中,我们需要先引入视频播放器组件才能使用它的功能。可以通过在页面的json

如何在uniapp中实现图片滤镜效果在移动应用开发中,图片滤镜效果是一种常见且受用户喜爱的功能之一。而在uniapp中,实现图片滤镜效果也并不复杂。本文将为大家介绍如何通过uniapp实现图片滤镜效果,并附上相关代码示例。导入图片首先,我们需要在uniapp项目中导入一张图片,以供后续滤镜效果的处理。可以在项目的资源文件夹中放置一张命名为“filter.jp

UniApp实现性能监控与瓶颈分析的最佳实践随着移动应用的快速发展,开发人员对应用性能的需求也日益增加。对于UniApp开发者来说,实现性能监控和瓶颈分析是非常重要的一项工作。本文将介绍UniApp中实现性能监控和瓶颈分析的最佳实践,并提供一些代码示例供参考。一、性能监控的重要性在现代移动应用中,用户体验是非常重要的。性能问题会导致应用加载速度慢、卡顿等问题

UniApp实现视频播放与录制的集成与使用技巧UniApp是一款跨平台的应用开发框架,可以用于开发微信小程序、H5站点、APP等多个平台。在UniApp中实现视频播放与录制是非常实用的功能之一。本文将介绍UniApp中如何集成和使用视频播放与录制的技巧,同时提供相应的代码示例。一、视频播放集成与使用在UniApp中实现视频播放可以使用uni-mp-video


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.