search
HomeWeChat AppletMini Program DevelopmentWeChat applet page jump and data transfer
WeChat applet page jump and data transferJun 23, 2018 pm 03:33 PM
WeChat appletPage jump

This article mainly introduces relevant information on WeChat applet page jumps and detailed examples of data transfer. Here are example codes to help you learn and understand at home. Friends in need can refer to

WeChat Mini program page jump and data transfer

1. Pilot

In Android, our Activity and Fragment both have the concept of stack in them. WeChat Mini The program page also has the concept of stack in it. There are four ways to jump to the WeChat applet page:

1.wx.navigateTo(OBJECT);
2.wx.redirectTo(OBJECT);
3.wx.switchTab( OBJECT);
4.wx.navigateBack(OBJECT)
5. Use to implement the corresponding jump function;

Analysis:

  1. where navigateTo is Save the original page in the page stack. When jumping to the next page, the target page is also pushed into the stack. Only in this case can you click the return button on the phone to jump to the previous page;

  2. RedirectTo and switchTab both clear the original page in the stack first, and then push the target page into the stack. Using these two jump methods, you cannot return to the previous page through the system's return key, but exit directly. Mini program;

  3. When using redirectTo, it must be used with the tabBar or the jump button in the page, otherwise you cannot return to the previous page;

  4. The page that switchTab jumps to must be the page declared in tabBar;

  5. The fields defined in tabBar cannot exceed 5 pages, and the page stack level of the applet cannot exceed 5 layers. .

  6. navigateBack can only return to the specified page in the page stack, and is generally used in conjunction with navigateTo.

  7. wx.navigateTo and wx.redirectTo are not allowed to jump to the tabbar page, only wx.switchTab can be used to jump to the tabbar page

2. Specific operations for page jump

(1)wx.navigateTo(OBJECT)

Keep the current page and jump Go to a page in the application and use wx.navigateBack to return to the original page.

Parameters Type Required Description
url String is the path of to the non-tabBar page in the application that needs to be jumped. Parameters can be included after the path. Parameters and paths are separated by ?, parameter keys and parameter values ​​are connected by =, and different parameters are separated by &; for example, 'path?key=value&key2=value2'
success Function No Callback function for successful interface call
fail Function No Callback function for interface call failure
complete Function No Callback function for end of interface call Function (executed successfully or failed)

Sample code:

wx.navigateTo({
 url: 'test?id=1'//实际路径要写全
})

##

//test.js
Page({
 onLoad: function(option){
 console.log(option.query) 
 }
})

Note: In order not to cause trouble to users when using the mini program, we stipulate that the page path can only be five levels. Please try to avoid multi-level interactions.


(2) wx.redirectTo(OBJECT)

Close the current page and jump to a page within the application.

ParametersTypeRequiredDescriptionurlString is the path of to the non-tabBar page in the application that needs to be jumped. Parameters can be included after the path. Parameters and paths are separated by ?, parameter keys and parameter values ​​are connected by =, and different parameters are separated by &; for example, 'path?key=value&key2=value2'successFunctionNoCallback function for successful interface callfailFunctionNo Callback function for interface call failurecompleteFunctionNoCallback function for end of interface call Function (executed successfully or failed) ##Sample code:

wx.redirectTo({
 url: 'test?id=1'
})

##( 3) wx.switchTab(OBJECT)

Jump to the tabBar page and close all other non-tabBar pages

OBJECT parameter description:

ParametersTypeRequiredDescriptionurlString is the path of the tabBar page that needs to be jumped (the page needs to be defined in the tabBar field of app.json). Parameters cannot be included after the path successFunctionNoCallback function for successful interface call failFunctionNoCallback function for failed interface callcompleteFunctionNoThe callback function at the end of the interface call (will be executed if the call is successful or failed)

示例代码:

{
 "tabBar": {
 "list": [{
  "pagePath": "index",
  "text": "首页"
 },{
  "pagePath": "other",
  "text": "其他"
 }]
 }
}

wx.switchTab({
 url: '/index'
})

(4)wx.navigateBack(OBJECT)

关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages()) 获取当前的页面栈,决定需要返回几层。

OBJECT 参数说明:

参数 类型 必填 说明
delta Number 1 返回的页面数,如果 delta 大于现有页面数,则返回到首页。

示例代码:

// 注意:调用 navigateTo 跳转时,调用该方法的页面会被加入堆栈,而 redirectTo 方法则不会。见下方示例代码

// 此处是A页面
wx.navigateTo({
 url: 'B?id=1'
})

// 此处是B页面
wx.navigateTo({
 url: 'C?id=1'
})

// 在C页面内 navigateBack,将返回A页面
wx.navigateBack({
 delta: 2
})

(5)使用标签实现页面跳转

navigator

页面链接。

参数 类型 必填 说明
url String
应用内的跳转链接
redirect Boolean false 打开方式为页面重定向,对应 wx.redirectTo(将被废弃,推荐使用 open-type)
open-type String navigate 可选值 ‘navigate'、'redirect'、'switchTab',对应于wx.navigateTo、wx.redirectTo、wx.switchTab的功能
hover-class String navigator-hover 指定点击时的样式类,当hover-class=”none”时,没有点击态效果
hover-start-time Number 50 按住后多久出现点击态,单位毫秒
hover-stay-time Number 600 手指松开后点击态保留时间,单位毫秒

示例代码:

<navigator url="navigate?title=navigate" hover-class="navigator-hover">跳转到新页面</navigator>
 <navigator url="redirect?title=redirect" open-type="redirect" hover-class="other-navigator-hover">在当前页打开</navigator>
 <navigator url="index" open-type="switchTab" hover-class="other-navigator-hover">切换 Tab</navigator>

3.页面的路由和生命周期

(1)页面的路由

在小程序中所有页面的路由全部由框架进行管理,对于路由的触发方式以及页面生命周期函数如下:





路由方式 触发时机 路由后页面 路由前页面
初始化 小程序打开的第一个页面 onLoad,onShow
打开新页面 调用 API wx.navigateTo 或使用组件 onLoad,onShow onHide
页面重定向 调用 API wx.redirectTo 或使用组件 onLoad,onShow onUnload
页面返回 调用 API wx.navigateBack 或用户按左上角返回按钮 onShow onUnload(多层页面返回每个页面都会按顺序触发onUnload)
Tab 切换 调用 API wx.switchTab 或使用组件 或用户切换 Tab 各种情况请参考下表

Tab 切换对应的生命周期(以 A、B 页面为 Tabbar 页面,C 是从 A 页面打开的页面,D 页面是从 C 页面打开的页面为例):

当前页面 路由后页面 触发的生命周期(按顺序)
A A Nothing happend
A B A.onHide(), B.onLoad(), B.onShow()
A B(再次打开) A.onHide(), B.onShow()
C A C.onUnload(), A.onShow()
C B C.onUnload(), B.onLoad(), B.onShow()
D B D.onUnload(), C.onUnload(), B.onLoad(), B.onShow()
D(从分享进入) A D.onUnload(), A.onLoad(), A.onShow()
D(从分享进入) B D.onUnload(), B.onLoad(), B.onShow()

4.参数传递

(1)通过路径传递参数

通过路径传递参数在wx.navigateTo(OBJECT)、wx.redirectTo(OBJECT)和中使用方法相同
示例代码:以wx.navigateTo为代表

"
wx.navigateTo({
 url: &#39;test?id=1&#39;//实际路径要写全
})

//test.js
Page({
 onLoad: function(option){
 console.log(option.id) 
 }
})

参数与路径之间使用?分隔,参数键与参数值用=相连,不同参数用&分隔;

test?id=1 中id为参数键,1 为参数值

在目的页面中onLoad()方法中option对象即为参数对象,可以通过参数键来取出参数值

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

微信小程序实现实时圆形进度条的方法

微信小程序 监听手势滑动切换页面的实现

The above is the detailed content of WeChat applet page jump and data transfer. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
微信小程序架构原理基础详解微信小程序架构原理基础详解Oct 11, 2022 pm 02:13 PM

本篇文章给大家带来了关于微信小程序的相关问题,其中主要介绍了关于基础架构原理的相关内容,其中包括了宿主环境、执行环境、小程序整体架构、运行机制、更新机制、数据通信机制等等内容,下面一起来看一下,希望对大家有帮助。

微信小程序云服务配置详解微信小程序云服务配置详解May 27, 2022 am 11:53 AM

本篇文章给大家带来了关于微信小程序的相关知识,其中主要介绍了关于云服务的配置详解,包括了创建使用云开发项目、搭建云环境、测试云服务等等内容,下面一起来看一下,希望对大家有帮助。

微信小程序常用API(总结分享)微信小程序常用API(总结分享)Dec 01, 2022 pm 04:08 PM

本篇文章给大家带来了关于微信小程序的相关知识,其中主要总结了一些常用的API,下面一起来看一下,希望对大家有帮助。

浅析微信小程序中自定义组件的方法浅析微信小程序中自定义组件的方法Mar 25, 2022 am 11:33 AM

微信小程序中怎么自定义组件?下面本篇文章给大家介绍一下微信小程序中自定义组件的方法,希望对大家有所帮助!

微信小程序实战项目之富文本编辑器实现微信小程序实战项目之富文本编辑器实现Oct 08, 2022 pm 05:51 PM

本篇文章给大家带来了关于微信小程序的相关知识,其中主要介绍了关于富文本编辑器的实战示例,包括了创建发布页面、实现基本布局、实现编辑区操作栏的功能等内容,下面一起来看一下,希望对大家有帮助。

西安坐地铁用什么小程序西安坐地铁用什么小程序Nov 17, 2022 am 11:37 AM

西安坐地铁用的小程序为“乘车码”。使用方法:1、打开手机微信客户端,点击“发现”中的“小程序”;2、在搜索栏中输入“乘车码”进行搜索;3、直接定位城市西安,或者搜索西安,点击“西安地铁乘车码”选项的“去乘车”按钮;4、根据腾讯官方提示进行授权,开通“乘车码”业务即可利用该小程序提供的二维码来支付乘车了。

微信小程序开发工具介绍微信小程序开发工具介绍Oct 08, 2022 pm 04:47 PM

本篇文章给大家带来了关于微信小程序的相关问题,其中主要介绍了关于开发工具介绍的相关内容,包括了下载开发工具以及编辑器总结等内容,下面一起来看一下,希望对大家有帮助。

订阅号跟小程序的区别是什么订阅号跟小程序的区别是什么Aug 25, 2022 pm 04:54 PM

订阅号跟小程序的区别:1、小程序加载小组件较慢,有专门提供的组件,而订阅号加载组件较快,但是时原生的页面组件;2、小程序的运营后台是实时数据,而订阅号则是非实时数据;3、小程序与聊天窗口之间可以相互切换,而订阅号则需要先退出才能进入聊天窗口;4、小程序的接口数量较多,而订阅号的接口数量较少,受限于浏览器,只提供有限的功能。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment