this.props.history.push 和 match 如何配合使用

发布于 2022-09-11 16:09:41 字数 2208 浏览 10 评论 0

问题是,我点击菜单切换子页面,但是没有成功。

如图图片描述

菜单 Materials click 事件
  clickMaterials = () => {
    this.props.history.push('/su');

    console.log("dddd  clickMaterials   ddddd");
    console.log(this.props.match.path);

  };

PrimaryLayout.js
......
  render() {
    const {component: Component, classes, ...rest } = this.props;

    return (
      <div className={classes.root}>
        <Route {...rest} path='/su' component={PrimaryLayout} />
import Suppliers from './Suppliers'
import WAppBar from './WAppBar'
import Dashboard from './Supplier/Dashboard'
import { checkPropTypes } from 'prop-types';

const PrimaryLayout = ({ match }) => (
  <div className="primary-layout">
    <main>
      <Switch>
        <Route path={`${match.path}`} exact component={Dashboard} />
        <Route path={`${match.path}/su`} component={Suppliers} />
        <Route path={`${match.path}/bar`} component={WAppBar} />
        <Redirect to={`${match.url}`} />
      </Switch>
      {console.log("PrimaryLayout")}
    </main>
  </div>
)

export default PrimaryLayout

Suppliers.js

/*function Suppliers(props) {*/
class Suppliers extends React.Component {  
  state = {
    open: false,
    selectedValue: "33333333",
  };

  handleClickOpen = () => {
    this.setState({
      open: true,
    });
 
    console.log(this.state.open);
  };

  handleClose = () => {
    this.setState({open: false });
  };

  render() {
    const { classes } = this.props;
    return (
      <div className={classes.root}>
        <STable/>
        <br/>
        <Button onClick={this.handleClickOpen} variant="contained" color="primary" className={classes.button}>Add</Button>
        <Edit open={this.state.open} onClose={this.handleClose}></Edit>
      </div>
    );
  }
}

Suppliers.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(Suppliers);

点击事件中 history.push('/su') , 期望是加载子页面suppliers,URL 都发生了变化,但是子页面不出来,变成空白页了。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文