在nextjs中获取URL路径名

reactjs React.js

神乐

2020-03-23

我有一个登录页面和布局组件。布局组件有标题。我不想在登录中显示标题。为此,我想获取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>
        )
    }
}

请帮忙

第2774篇《在nextjs中获取URL路径名》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

1个回答
2020.03.23

您可以使用window.locationcomponentDidMount()或者componentDidUpdate()是这样的:

componentDidMount() {
  this.currentUrl = window.location;
}

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android