Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this.props.history后路径发生变化了,但内容没有变化 #19

Closed
ye-carol opened this issue May 27, 2019 · 4 comments
Closed
Labels
help wanted Extra attention is needed

Comments

@ye-carol
Copy link

ye-carol commented May 27, 2019

image
#12 (comment)
我按照这个配置页面可以使用Link了,但this.props.history后路径发生变化了,但内容没有变化

0{ZMACLC$Z$2A%K4KEUCMM4
如果是这样嵌套的话this.history.props是有效的,但页面使用Link报错

@ShenChang618
Copy link
Member

@ye-carol 可以发一份 DEMO 给我吗?我来找下原因😁

@NARUTOne
Copy link

image

image

image

通过link点击可以跳转,但是遇到多级菜单,出现图三问题
image

import React, {useState} from 'react';
import ReactDOM from 'react-dom';
import {
  Switch,
  Route,
  Link,
  BrowserRouter as Router,
} from 'react-router-dom';
import {Provider, KeepAlive} from '../../es';
import A from './views/A';
import B from './views/B';
import C from './views/C';
import D from './views/D';

function App() {
  const [toggle, setToggle] = useState(true);
  return (
    <div>
      <ul>
        <li>
          <Link to="/a">a</Link>
        </li>        
        <li onClick={() => setToggle(true)}>
          <Link to="/b">b</Link>
        </li>
        <li onClick={() => setToggle(false)}>
          <Link to="/c">c</Link>
        </li>
        <li>
          <Link to="/d/1">d1</Link>
        </li>
        <li>
          <Link to="/d/2">d2</Link>
        </li>
      </ul>

      <div>
        <button onClick={() => setToggle(!toggle)}>toggle({toggle.toString()})</button>
      </div>

      <Switch>
        <Route
          path="/a"
          render={() => (
            <KeepAlive name="A" >
              <A />
            </KeepAlive>
          )}
        />
        <Route
          path="/b"
          render={() => (
            <KeepAlive name="B" disabled={!toggle}><B /><B /></KeepAlive>
          )}
        />
        <Route
          path="/c"
          render={() => (
            <KeepAlive name="C">
              <C />
            </KeepAlive>
          )}
        />
        <Route
          path="/d/1"
          exact
          render={() => (
            <KeepAlive name="D1">
              <D />
            </KeepAlive>
          )}
        />
        <Route
          path="/d/2"
          exact
          render={() => (
            <KeepAlive name="D2">
              <D />
            </KeepAlive>
          )}
        />
      </Switch>
    </div>
  );
}

ReactDOM.render(
  (
    
      <Router>
        <Provider include={/D\d/}><App /></Provider>        
      </Router>
    
  ),
  document.getElementById('root')
);

@NARUTOne
Copy link

看了半天,没看出问题,大神给看看呗 😢

@ShenChang618
Copy link
Member

@NARUTOne 抱歉,这个是我项目 Webpack 少配置了东西

const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');
const webpack = require('webpack');

const ROOT = path.join(__dirname, 'demo');
const SRC = path.join(ROOT, 'src');

module.exports = {
  mode: 'development',
  devtool: 'source-map',
  entry: {
    index: path.join(SRC, 'index.js'),
  },
  output: {
    path: path.join(ROOT, 'build'),
    filename: 'static/[name].js',
    // 需要配置公共路径
    publicPath: '/',
  },
  plugins: [
    new HtmlWebpackPlugin({
      filename: 'index.html',
      inject: true,
      template: path.join(ROOT, 'index.html'),
    }),
  ],
  module: {
    rules: [
      {
        test: /\.js$/,
        use: ['babel-loader'],
        include: SRC,
      },
    ],
  },
};

@ShenChang618 ShenChang618 added the help wanted Extra attention is needed label May 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants