-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-router-history.js
58 lines (55 loc) · 1.64 KB
/
react-router-history.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* react router中在js代码中执行router跳转,可采用history库。多多参考https://reacttraining.com/react-router/
*/
//----------------App.js------------//
// some necessary import;
import createHistory from 'history/createBrowserHistory';
const history = createHistory({
basename: '/views' // 总共有三种URL配置形式
});
// this history is a global variable, it will use in children component
export {history};
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
render() {
return (
<Router history={history}>
<Switch>
<Route exact path="/" component={Note}/>
<Route path="/login" component={Login}/>
<Route path="/calendar" component={Calendar}/>
<Route path="/my" component={MyBook}/>
<Route path="/status" component={BookStatus}/>
<Route path="/booking" component={AdminBooking}/>
</Switch>
</Router>
);
}
}
//--------------------------login.js------------------------//
import {history} from '../App.js'
function login() {
return system.ajax({
'method': 'post',
'url': 'http://xxx',
'data': {
'username': username,
'password': password,
}
})
.then(res => {
//
if (this.props.location && this.props.location.state) {
history.replace(this.props.location.state.repath || '/');
} else {
history.replace('/');
}
})
.catch(reason => {
console.log(reason)
console.log('cowutiui')
});
}