【问题标题】:Typescript Chain Async Call打字稿链异步调用
【发布时间】:2022-11-17 23:08:33
【问题描述】:

我需要:

localStorage.setItem('用户', 用户); router.navigate(['/profile']);

问题是导航可能发生在存储设置之前。

请告诉我,我不需要路由参数、创建异步类、回调、承诺或计时器来使其工作。

【问题讨论】:

  • localStorage 是同步的...

标签: typescript asynchronous call chain


【解决方案1】:

您可以像这样在 useEffect 挂钩中设置 localstorage 值:

 React.useEffect(()=>{ 
   setStorage()
   router.navigate(['/profile'])      
 },[])
 
 const setStorage = ()=>{  
    localStorage.setItem("user", user)  
 }

我将在 router.navigate 之前在 localstorage 中设置值

【讨论】:

    猜你喜欢
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 2018-05-25
    • 2018-05-10
    • 2019-03-25
    • 1970-01-01
    • 2023-02-14
    相关资源
    最近更新 更多