【发布时间】:2022-11-17 23:08:33
【问题描述】:
我需要:
localStorage.setItem('用户', 用户); router.navigate(['/profile']);
问题是导航可能发生在存储设置之前。
请告诉我,我不需要路由参数、创建异步类、回调、承诺或计时器来使其工作。
【问题讨论】:
-
localStorage 是同步的...
标签: typescript asynchronous call chain
我需要:
localStorage.setItem('用户', 用户); router.navigate(['/profile']);
问题是导航可能发生在存储设置之前。
请告诉我,我不需要路由参数、创建异步类、回调、承诺或计时器来使其工作。
【问题讨论】:
标签: typescript asynchronous call chain
您可以像这样在 useEffect 挂钩中设置 localstorage 值:
React.useEffect(()=>{
setStorage()
router.navigate(['/profile'])
},[])
const setStorage = ()=>{
localStorage.setItem("user", user)
}
我将在 router.navigate 之前在 localstorage 中设置值
【讨论】: