我剛纔用反應路由器V4開始,我想知道如何讓路由器陣營路由器V4獲得當前位置
的當前位置,這是我的源代碼
import {Meteor} from 'meteor/meteor';
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import createHistory from 'history/createBrowserHistory'
import {Route, BrowserRouter as Router, Switch} from 'react-router-dom'
import {Tracker} from 'meteor/tracker';
import Signup from '../imports/ui/signUp';
import Link from '../imports/ui/link';
import Login from '../imports/ui/login';
import NotFound from '../imports/ui/notFound';
const history = createHistory();
const unauthenticatedPages = ['/', '/signup'];
const authenticatedPages = ['/links'];
const routes = (
<Router history={history}>
<Switch>
<Route exact path="/" component={Login}/>
<Route exact path="/signup" component={Signup}/>
<Route path="/links" component={Link}/>
<Route component={NotFound}/>
</Switch>
</Router>
);
Tracker.autorun(() => {
const unlisten = history.listen((location, action) => {
// location is an object like window.location
console.log(action, location.pathname, location.state)
})
const isAuthenticated = !!Meteor.userId();
console.log('location: ', location.pathname);
//const pathName =
});
Meteor.startup(() => {
ReactDOM.render(routes, document.getElementById('app'));
});
我根據試反應路由器文檔使用歷史,但我沒有得到的位置。
如何獲取路線的當前位置?
我不使用REDX,我會很感激沒有它的答案。
謝謝,邁克爾。
在您的路線組件中,您可以將匹配道具傳遞給子組件並訪問那裏的位置。這種方法被用來鏈接路由組件 –