子组件如何获得父组件的history

withRouter将react-router 的 history、location、match 三个对象传入props对象上,
通过withRouter包裹一下子组件 子组件就可以获得react-router 的 history

父组件中需要传入 historys(一个名字)
<GoodFoot historys={this.props.history}> </GoodFoot>

子组件中导入withRouter
import { withRouter } from ‘react-router-dom’

包裹子组件
export default withRouter(子组件名字)
然后就可以使用react-router 的history了
this.props.historys.push(’/路径名称’)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue3,可以使用Vue Router来实现根据路由加载组件。具体步骤如下: 1. 安装Vue Router 使用npm或yarn安装Vue Router ``` npm install vue-router ``` 2. 创建Vue Router实例 在main.js文件创建Vue Router实例,并将其挂载到Vue实例。 ``` import { createApp } from 'vue' import App from './App.vue' import router from './router' const app = createApp(App) app.use(router) app.mount('#app') ``` 3. 创建路由配置 在router.js创建路由配置,指定每个路由对应的组件。 ``` import { createRouter, createWebHistory } from 'vue-router' import Home from './components/Home.vue' import About from './components/About.vue' import Contact from './components/Contact.vue' const routes = [ { path: '/', component: Home }, { path: '/about', component: About }, { path: '/contact', component: Contact } ] const router = createRouter({ history: createWebHistory(), routes }) export default router ``` 4. 在组件使用路由 在需要加载组件组件使用Vue Router提供的router-view组件来显示组件。 ``` <template> <div> <h1>My App</h1> <router-view></router-view> </div> </template> ``` 5. 导航到其他路由 在需要导航到其他路由的组件使用Vue Router提供的router-link组件来生成链接。例如: ``` <template> <div> <h2>About</h2> <p>This is the about page.</p> <router-link to="/">Go back to home page</router-link> </div> </template> ``` 当用户点击“Go back to home page”链接时,将会导航到根路由对应的组件(即Home组件)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值