全局注入history

当我们想在子组件中使用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')

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 包含 react基础 react传值 react路由 和redux管理 和react-redux reactDom...
    栀璃鸢年_49a3阅读 4,819评论 0 1
  • 渲染函数和jsx 在vue中我们可以不用template来指定组件的模板,而是用render函数来创建虚拟dom结...
    6e5e50574d74阅读 3,987评论 0 0
  • 3. JSX JSX是对JavaScript语言的一个扩展语法, 用于生产React“元素”,建议在描述UI的时候...
    pixels阅读 7,911评论 0 24
  • Vue八个生命周期 beforeCreate【创建前】created【创建后】 beforeMount【载入前】 ...
    艾萨克菊花阅读 5,157评论 0 12
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,150评论 0 10