当前位置: 首页 > 知识库问答 >
问题:

React路由器V4获取当前路径

何乐
2023-03-14

如何使用react router v4获取当前路径?

我尝试了以下方法,但没有成功:

const currentLocation = this.props.location.pathname;

错误:无法读取未定义的属性“pathname”

这是我的Routes.js文件:

import React, {Component} from 'react';
import {  BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import { Provider } from 'react-redux';
import configureStore from './Store';
import LevelOne from './containers/LevelOne';
import LevelTwo from './containers/LevelTwo';
import LevelThree from './containers/LevelThree';
import CreateProfile from './containers/Profile/CreateProfile';
import WhosWatching from './containers/Profile/WhosWatching';
import ProfileNameAvatar from './containers/Profile/ProfileNameAvatar';
import FavouriteGenres from './containers/Profile/FavouriteGenres';
import FourZeroFour from './containers/404';

import Header from './components/Header';

const store = configureStore();

const navItems = [
  {
    title:"For You",
    to: "/for-you"
  },
  {
    title:"Movies",
    to: "/movies"
  },
  {
    title:"Series",
    to: "/series"
  },
  {
    title:"TV Shows",
    to: "/tv-Shows"
  },
  {
    title:"Subscriptions",
    to: "/subscriptions"
  },
  {
    title:"Live TV",
    to: "/live-tv"
  }
]

export default class Routes extends Component {
  state = {
    theme: 'light'
  }

  header = React.createRef();

  setTheme = (theme) => {
    this.setState({
      theme: theme,
    });
  }

  render() {
    const currentLocation = this.props.location.pathname;
    console.log("location", currentLocation);
    return (
      <Provider store={store}>
        <Router ref='router'>
          <div className='app'>
            {/*<Header navItems={navItems} theme={this.state.theme} ref={this.header} />*/}
            <Switch>
              <Route exact path="/" render={(props) => (
                <LevelOne {...props} setTheme={this.setTheme} />
              )}/>
              <Route exact path="/for-you" render={(props) => (
                <LevelTwo {...props} setTheme={this.setTheme} />
              )}/>
              <Route exact path="/for-you/view-all" render={(props) => (
                <LevelThree {...props} setTheme={this.setTheme} innerRef={this.header} />
              )}/>
              <Route exact path="/profile/create-profile" render={(props) => (
                <CreateProfile {...props} />
              )}/>
              <Route exact path="/profile/whos-watching" render={(props) => (
                <WhosWatching {...props} />
              )}/>
              <Route exact path="/profile/profile-name-avatar" render={(props) => (
                <ProfileNameAvatar {...props} />
              )}/>
              <Route exact path="/profile/favourite-genres" render={(props) => (
                <FavouriteGenres {...props} />
              )}/>
              <Route component={FourZeroFour} />
            </Switch>
          </div>
        </Router>
      </Provider>
    );
  }
}

共有3个答案

秦安怡
2023-03-14

这个.props.match.path应该可以做到这一点

巩子实
2023-03-14

快速解决方案
在函数上创建一个变量:

var currentLocation = window.location.pathname;

然后在ul中

它为mee工作,是一个快速的解决方案。

章飞虎
2023-03-14

您只能访问路由组件中的this.props.location(当道具从组件传递到组件时)。

您可以使用路由器外部的window.location.pathname获取当前路径,但如果您在此处访问路径,则很可能是做错了什么。

对于路由器的子级,您可以通过使用路由器将其包裹在周围来访问该位置,或者从路由组件向下传递道具。

Redux集成还允许您从任何地方访问该位置。

 类似资料:
  • 问题内容: 我想显示在以某种方式从目前的路线通过。 在React Router v4中,如何将当前路由传递到prop中? 有没有办法通过从自定义自定义标题上? 问题答案: 在5.1版本的react-router中,有一个名为 useLocation 的钩子,该钩子返回当前的位置对象。每当您需要知道当前URL时,这可能都会有用。

  • 我有一个路由器像下面: 以下是我想要实现的目标: 如果未登录,将用户重定向到 如果用户在登录后试图访问,请将其重定向到root 因此,现在我尝试在

  • 我在想这样的事情: 前台有一个不同的布局和风格与管理区域。所以在frontpage中的路由是home、about等等,其中一个应该是子路由。 /home应该呈现到Frontpage组件中,而/admin/home应该呈现在后端组件中。 最终解决方案: 这是我现在使用的最终解决方案。这个例子也像传统的404页面一样有一个全局错误组件。

  • 在此URL中: 我想抓,当然是在路由里面。这工作很棒: 但是我想访问任何路由之外的路径以及文件如下:

  • 我在从React路由器v3迁移到v4时遇到了一些小问题。在v3中,我可以在任何地方执行此操作: 如何在v4中实现这一点。 我知道,当您在组件中时,我可以使用hoc、react context或event router道具。但我不是这样。 我正在寻找v4中组件的外部导航的等价性