dvajs / dva

🌱 React and redux based, lightweight and elm-style framework. (Inspired by elm and choo)
https://dvajs.com/
MIT License
16.24k stars 3.17k forks source link

用history.listen监听时为什么会直接callback一次history实例呢? #2422

Closed wuyiqun0718 closed 3 years ago

wuyiqun0718 commented 3 years ago

https://github.com/dvajs/dva/blob/fa5b8a4809d92466c8c2950d4235855830d37c1e/packages/dva/src/index.js#L106-#L125

在我们的项目中引入dva中的history想用来监听路由变化,但是看源代码发现监听时就会直接callback闭包里的history实例,这个不符合常理吧?原生的history是没有做这样的处理的。

hopperhuang commented 3 years ago

因为第一次加载model的时候才会将history监听器加入监听,后续进入同样的路径,监听器才会触发。所以要在加入监听器时,同时触发一次。不然第一次进去一个页面,history监听器就不会触发了。

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.