We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
H5
https://github.com/lblblong/taro-h5-onunload-issue 浏览器版本: Edge 90.0.818.46 使用框架: React
进入主页后进入第二个页面,在第二个页面返回时,onUnload 生命周期中调用 Taro.getCurrentInstance 获取不到 page 和 router
编译到 小程序环境就可以获取到,h5不行
h5 Page.onUnload 通过 Taro.getCurrentInstance 获取到 page 和 router
获取不到
Taro v3.2.1 Taro CLI 3.2.1 environment info: System: OS: Windows 10 Binaries: Node: 14.15.4 - E:\app\nodejs\.nvm\v14.15.4\node.EXE Yarn: 1.22.10 - E:\app\nodejs\.nvm\v14.15.4\yarn.CMD npm: 6.14.10 - E:\app\nodejs\.nvm\v14.15.4\npm.CMD
The text was updated successfully, but these errors were encountered:
@lblblong 闭包起来用吧:
import React, { Component } from "react"; import { View, Text, Button } from "@tarojs/components"; import "./index.css"; import Taro from "@tarojs/taro"; export default class Index extends Component { componentWillMount() { // debugger const instance = Taro.getCurrentInstance() const pageConfig = instance.page const originOnUnload = pageConfig.onUnload; pageConfig.onUnload = function () { // debugger console.log("页面卸载", pageConfig); originOnUnload.apply(this); }; } componentDidMount() {} componentWillUnmount() {} componentDidShow() {} componentDidHide() {} render() { return ( <View className="index"> <Text>Hello world!</Text> <Button onClick={() => { Taro.navigateBack(); }} > 返回 </Button> </View> ); } }
微信小程序 onUnLoad 触发前不会触发 onHide,H5 则规定 onUnLoad 触发前会触发 onHide。而 onHide 时内部会把 instance.page 置为 null:
onUnLoad
onHide
taro/packages/taro-runtime/src/dsl/common.ts
Lines 181 to 187 in a87ad10
Sorry, something went wrong.
有一些外部代码,比如第三方库调用 Taro.getCurrentInstance 时怎么办呢,为什么不让 h5 和小程序行为一致
No branches or pull requests
相关平台
H5
复现仓库
https://github.com/lblblong/taro-h5-onunload-issue
浏览器版本: Edge 90.0.818.46
使用框架: React
复现步骤
进入主页后进入第二个页面,在第二个页面返回时,onUnload 生命周期中调用 Taro.getCurrentInstance 获取不到 page 和 router
编译到 小程序环境就可以获取到,h5不行
期望结果
h5 Page.onUnload 通过 Taro.getCurrentInstance 获取到 page 和 router
实际结果
获取不到
环境信息
The text was updated successfully, but these errors were encountered: