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 React路由器V4获取当前路径_Javascript_Reactjs_React Router - Fatal编程技术网

Javascript React路由器V4获取当前路径

Javascript React路由器V4获取当前路径,javascript,reactjs,react-router,Javascript,Reactjs,React Router,如何使用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-do

如何使用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>
    );
  }
}
import React,{Component}来自'React';
从“react Router dom”导入{BrowserRouter as Router,Route,Switch};
从'react redux'导入{Provider};
从“/Store”导入配置存储;
从“./containers/LevelOne”导入LevelOne;
从“/containers/LevelTwo”导入LevelTwo;
从“/containers/LevelThree”导入LevelThree;
从“/containers/Profile/CreateProfile”导入CreateProfile;
从“/containers/Profile/WhosWatching”导入WhosWatching;
从“./containers/Profile/ProfileNameAvatar”导入ProfileNameAvatar;
从“/containers/Profile/favoriteGenes”导入收藏夹类型;
从“./containers/404”导入FourZeroFour;
从“./components/Header”导入标题;
const store=configureStore();
常数navItems=[
{
标题:“为了你”,
致:“/为你”
},
{
标题:“电影”,
至:“/电影”
},
{
标题:“系列”,
至:“/系列”
},
{
标题:“电视节目”,
致:“/电视节目”
},
{
标题:“订阅”,
至:“/订阅”
},
{
标题:“直播电视”,
致:“/直播电视”
}
]
导出默认类路由扩展组件{
状态={
主题:“光”
}
header=React.createRef();
setTheme=(主题)=>{
这是我的国家({
主题:主题,,
});
}
render(){
const currentLocation=this.props.location.pathname;
console.log(“位置”,currentLocation);
返回(
{/**/}
(
)}/>
(
)}/>
(
)}/>
(
)}/>
(
)}/>
(
)}/>
(
)}/>
);
}
}

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

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

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

对于
路由器
的子级,您可以通过环绕该位置或从
路由
组件向下传递道具来访问该位置

还将允许您从任何位置访问该位置

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

var currentLocation = window.location.pathname;
然后在
ul>li className={(currentLocation=='/'?'active':'')中


它适用于mee,是一个快速解决方案。您可以直接从
文档
变量获取路径


例如:
const path=document.location.pathname

谢谢您的回答,我用您的建议替换了currentLocation const,并得到一个错误:无法读取undefinedSorry的属性“path”,我的命令是在路由器组件内部使用的,我相信谢谢你@riwu-这正是我所追求的。如果你想用一个引用(如密钥或名称)检索确切的路由,请检查如果你的组件在路由器之前,而不能在路由器内部,该怎么办?它不会生成