Skip to content
New issue

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

是否支持全局生命周期钩子 #8213

Closed
bibidu opened this issue Dec 7, 2020 · 8 comments
Closed

是否支持全局生命周期钩子 #8213

bibidu opened this issue Dec 7, 2020 · 8 comments
Labels
enhancement New feature or request
Milestone

Comments

@bibidu
Copy link

bibidu commented Dec 7, 2020

这个特性解决了什么问题?

希望可以配置onLoad、onReady生命周期钩子,并在所有页面执行对应生命周期时,执行公共逻辑

这个 API 长什么样?

Taro.lifetimes.intercetpor('onLoad', (pageInstance) => {

})

@taro-bot2 taro-bot2 bot added the enhancement New feature or request label Dec 7, 2020
@bibidu
Copy link
Author

bibidu commented Dec 7, 2020

由于 document.body 拿到的始终是第一个页面的实例,即使跳转到另一个页面,通过document.body.appendChild仍然添加到上一个页面,所以希望可以通过配置生命周期钩子,将每个页面的实例拿到,然后再添加节点到当前页面实例

// pageA
import XUI from 'xui'

export default {
  methods: {
    showToast() {
      XUI.$toast('pageA')
    }
  }
}

// pageB (从pageA跳转而来)
import XUI from 'xui'

export default {
  methods: {
    showToast() {
      // 此时动态创建的元素被添加到了 pageA,期望添加到当前页面
      XUI.$toast('pageB')
    }
  }
}```

@Chen-jj
Copy link
Contributor

Chen-jj commented Dec 11, 2020

onLaunch、onLoad、onReady 生命周期都是支持的,直接用就行。

@tranter1991
Copy link

@Chen-jj 可以支持下useLaunch, useLoad hooks么, 这样就可以在其他hooks里面里面用

@tranter1991
Copy link

或者类似useAppEvent('onLoad', cb) 这样

@yangger6
Copy link

看看这个能满足需求不 mini-lifecycle-interceptor

@Chen-jj
Copy link
Contributor

Chen-jj commented Jun 2, 2022

Taro v3.5 开始支持 useLaunch useLoad useUnload

@Chen-jj Chen-jj closed this as completed Jun 2, 2022
@Chen-jj Chen-jj added this to the 3.5.0 milestone Jun 2, 2022
@AEPKILL
Copy link

AEPKILL commented Jun 10, 2022

@Chen-jj 可以支持下useLaunch, useLoad hooks么, 这样就可以在其他hooks里面里面用

可以先这样写:

const App = forwardRef((props, ref: any) => {
  useImperativeHandle(
    ref,
    () => {
      return {
        onLaunch() {
          console.log("2333333");
        },
      };
    },
    []
  );

  return props.children
});

export default App;

我看 Taro 源码就是拿 Ref 然后调用 Ref 上的 onLaunch

@dzcpy
Copy link

dzcpy commented Dec 12, 2022

useLaunch 不好用,写进首页不会执行

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants