打赏作者
感谢打赏,创作不易~
  • 微信
  • 支付宝

React+ReactToolKit中,如何在组件外部访问Redux的store,Uncaught ReferenceError: Cannot access slice before initialization at

之前在项目开发中,遇到一个问题,需要在axios的二次封装文件中,进行拦截,需要使用到redux的dispath派发

但是在axios封装的文件中,直接引入store,出现了如下报错:

Uncaught ReferenceError:  Cannot access "loginslice" before initialization at

解决方案如下:

在axios封装的文件中,创建一个变量,再创建一个函数,函数接收一个参数store,将函数参数接收的store,赋值给变量,抛出函数,在项目的入口文件中,引入刚刚抛出的函数,并调用,传入store,此时,就可以拿到redux的store了

参考链接:

https://redux.js.org/faq/code-structure#how-can-i-use-the-redux-store-in-non-component-files

posted @ 2023-03-03 09:42  Lanny-Chung  阅读(281)  评论(0编辑  收藏  举报