我有一个登录页面和布局组件。布局组件有标题。我不想在登录中显示标题。为此我想获取 url 路径名。基于路径名显示标题。
import * as constlocalStorage from '../helpers/localstorage';
import Router from 'next/router';
export default class MyApp extends App {
componentDidMount(){
if(constlocalStorage.getLocalStorage()){
Router.push({pathname:'/app'});
} else{
Router.push({pathname:'/signin'});
}
}
render() {
const { Component, pageProps } = this.props
return (
//I want here pathname for checking weather to show header or not
<Layout>
<Component {...pageProps} />
</Layout>
)
}
}
请帮忙
原文由 Karthi 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果您想在应用程序的任何功能组件中访问
router
对象,可以使用useRouter
挂钩,以下是使用方法:https://nextjs.org/docs/api-reference/next/router#userouter