Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在nextjs类组件中以编程方式导航到路由?_Javascript_Reactjs_Next.js_Router - Fatal编程技术网

Javascript 如何在nextjs类组件中以编程方式导航到路由?

Javascript 如何在nextjs类组件中以编程方式导航到路由?,javascript,reactjs,next.js,router,Javascript,Reactjs,Next.js,Router,我正在使用nextjs构建一个简单的应用程序。我的大多数组件都是功能组件。然而,我有一个类组件来处理表单 我想在提交表单后重定向到主页console.log(this.router)提供未定义的。因为它是一个类组件,所以我不能使用useRouter()hook。如何在next.js中引用类内路由器组件?最后我得到了答案 import Router from 'next/router' 并使用Router.push(…)作为直接使用next/Router默认导出的替代方法,withRouterH

我正在使用nextjs构建一个简单的应用程序。我的大多数组件都是功能组件。然而,我有一个类组件来处理表单


我想在提交表单后重定向到主页
console.log(this.router)
提供未定义的。因为它是一个类组件,所以我不能使用
useRouter()
hook。如何在next.js中引用类内路由器组件?

最后我得到了答案

import Router from 'next/router'

并使用
Router.push(…)
作为直接使用
next/Router
默认导出的替代方法,
withRouter
HOC还可以将路由器对象添加到任何组件

import*作为来自“React”的React
从“下一个/路由器”导入{withRouter}
类页扩展了React.Component{
render(){
const{router}=this.props
返回{router.pathname}

} } 使用路由器导出默认值(第页)