全局注入history

作者: 燕自浩 | 来源:发表于2019-11-04 11:58 被阅读0次

当我们想在子组件中使用history时却发现提示history不存在有两种解决方法

1. 由父组件向子组件传递history,比如

父组件:

a. <Parent {...props} /> 这样注入的是所有的不只包含了history

b. <Parent history = {this.props.history} /> 单一注入history

然后在子组件直接this.props.history 使用即可

2. 全局注入

a. 引入模块import { createHashHistory } from 'history'

b. 使用的地方

const history = createHashHistory()

 history.replace('/preserve')

这样就可以使用了,文章虽短希望能够帮助大家欢迎回帖交流技术,如果大家有好的方法一定要互动哦

相关文章

网友评论

    本文标题:全局注入history

    本文链接:https://www.haomeiwen.com/subject/wqorbctx.html