Przeglądaj źródła

改一下跳转

kulley 6 lat temu
rodzic
commit
7c3d070bee
3 zmienionych plików z 38 dodań i 22 usunięć
  1. 22 8
      src/App.js
  2. 4 1
      src/index.js
  3. 12 13
      src/pages/cart/empty/index.js

+ 22 - 8
src/App.js

@@ -5,7 +5,7 @@ import Home from './pages/home'
 import Cart from './pages/cart'
 import My from './pages/my'
 import './app.css'
-import {BrowserRouter as Router, Switch, Route, NavLink} from 'react-router-dom'
+import {Switch, Route, NavLink, withRouter} from 'react-router-dom'
 import classnames from 'classnames'
 
 class App extends Component {
@@ -35,14 +35,18 @@ class App extends Component {
     render() {
         let {selectedTab, tabHidden, page} = this.state
         return (
-            <Router>
+
                 <div>
                     <div className={classnames('tabbar', {'tarbar-hidden': tabHidden})}>
                         <Row>
-                            {/*navlink 的 activeClass 有点问题*/}
                             <NavLink exact to="/">
                                 <Col className={this.chooseClassNames('home')} span={8} onClick={() => {
-                                    this.changeTabBar('home')
+                                    this.props.history.push({
+                                        pathname: '/home'
+                                    })
+                                    this.setState({
+                                        selectedTab: 'home'
+                                    })
                                 }}>
                                     {
                                         selectedTab === 'home' ?
@@ -57,7 +61,12 @@ class App extends Component {
                             </NavLink>
                             <NavLink to="/cart">
                                 <Col className={this.chooseClassNames('cart')} span={8} onClick={() => {
-                                    this.changeTabBar('cart')
+                                    this.props.history.push({
+                                        pathname: '/cart'
+                                    })
+                                    this.setState({
+                                        selectedTab: 'cart'
+                                    })
                                 }}>
                                     {
                                         selectedTab === 'cart' ?
@@ -72,7 +81,12 @@ class App extends Component {
                             </NavLink>
                             <NavLink to="/my">
                                 <Col className={this.chooseClassNames('my')} span={8} onClick={() => {
-                                    this.changeTabBar('my')
+                                    this.props.history.push({
+                                        pathname: '/my'
+                                    })
+                                    this.setState({
+                                        selectedTab: 'my'
+                                    })
                                 }}>
                                     {
                                         selectedTab === 'my' ?
@@ -94,12 +108,12 @@ class App extends Component {
                         <Route path="/my" component={My}/>
                     </Switch>
                 </div>
-            </Router>
+
         )
     }
 }
 
-export default App
+export default withRouter(App)
 
 const HomeUnselectedIcon = () => (
     <Icon type="home" style={{fontSize: 22}}/>

+ 4 - 1
src/index.js

@@ -6,6 +6,7 @@ import ApolloClient from "apollo-boost"
 import {ApolloProvider} from "react-apollo"
 
 import {graphqlFC} from "./configs/url"
+import {BrowserRouter as Router} from 'react-router-dom'
 
 const client = new ApolloClient({
     uri: graphqlFC
@@ -13,7 +14,9 @@ const client = new ApolloClient({
 
 ReactDOM.render(
     <ApolloProvider client={client}>
-        <App/>
+        <Router>
+            <App/>
+        </Router>
     </ApolloProvider>,
     document.getElementById('root')
 )

+ 12 - 13
src/pages/cart/empty/index.js

@@ -1,31 +1,30 @@
-import React, {Component} from 'react';
-import {withRouter} from "react-router-dom";
+import React, {Component} from 'react'
+import {withRouter} from "react-router-dom"
 
-import cart_empty from '../../../images/cart_empty.jpg';
-import './index.css';
+import cart_empty from '../../../images/cart_empty.jpg'
+import './index.css'
 
 class Empty extends Component {
     constructor(props) {
-        super(props);
+        super(props)
         this.state = {}
     }
 
     render() {
-        let contentHeight = window.innerHeight - 95;
+        let contentHeight = window.innerHeight - 95
 
         return (
-            <div className="cart-empty" style={{height:contentHeight}} >
+            <div className="cart-empty" style={{height: contentHeight}}>
                 <div>
                     <img src={cart_empty} alt="img" width={100}/>
                 </div>
                 <div>购物车空空如此</div>
                 <div>
                     <button className="empty-button"
-                            onClick={()=>{
-                                window.location = '/#tab=my'
-                            //     this.props.history.push({
-                            //         pathname: `/`
-                            // })
+                            onClick={() => {
+                                this.props.history.push({
+                                    pathname: `/`
+                                })
                             }}>
                         去逛逛
                     </button>
@@ -35,4 +34,4 @@ class Empty extends Component {
     }
 }
 
-export default withRouter(Empty);
+export default withRouter(Empty)