0

问题:

此动态路由器有效,除非存在涉及参数的动态链接。

具体来说:

我可以硬编码链接或在浏览器中输入:

 <Link to="Inventory/All-Vehicles">All Vehicles</Link>
  http://localhost:3000/Inventory/All-Vehicles

并带有代码:

常量 { id } = this.props.params;
控制台.log({ id } );

控制台显示:

{id:“所有车辆”}

但是,使用动态链接...

< Link to={ this.props.item.linkTo } className="">{ this.props.item.title }< /Link>

产生:

< a class="" href="#Inventory/All-Vehicles" data-reactid=".0.0.0.0.0.0.0.0.0.0.$2.1.0.$0.0.0.0">All Vehicles< /a>

浏览器显示

localhost:3000/#/Inventory/All-Vehicles

片刻,然后将自身重置为(页面不重新加载)

本地主机:3000/#/库存

控制台显示:

对象{id:未定义}

我根据乔丹在下面的建议重写了这个问题。
我希望它不会太长。我使用 alt Flux 作为我的商店

路线.js:

import React, { Component, PropTypes } from 'react';
import Router, { Route } from 'react-router';
// alt
import connectToStores from '../../node_modules/alt/utils/connectToStores';
import NavStore from '../alt/stores/nav-store';
import NavActions from '../alt/actions/nav-actions';

class Routes extends Component {

  constructor(props) {
    super(props);
    this.state = {
      routes: []
    };
  }

  static getStores() {
    return [NavStore];
  }

  static getPropsFromStores() {
    return NavStore.getState();
  }

  componentDidMount() {
    const clientId = this.props.clientId;
    NavActions.getAll(clientId);
  }

  fetchNonRootComponent(paths) {
    let result;
    paths.map((path) => {
      if (path !== '/') {
        result = path;
      }
    });
    return result;
  }

  fetchMenuSystem(data) {
    const self = this;
    const currRoutesState = this.state.routes;
    const routes = data === undefined ? this.props.items : data;

    routes.map((route) => {
      // set paths up first
      let currPaths = [];
      if (route.paths !== undefined) {
        currPaths = route.paths;
      } else {
        currPaths.push(route.linkTo);
      }
      // Components - first check for ecomMods
      let currComponent;

如果它在 routes.js 文件中,那么这部分可能需要做一些事情:

      if (route.ecomMod !== undefined) {
        currComponent = require('../components/pages/' + route.ecomMod);
        // clear out currPath if this is an ecom Module
        // and start a new currPaths array
        currPaths = [];
        if (route.parentId === null) {
          currPaths.push(route.ecomMod);
        } else {

          // multi-purpose :id, eg.
          // Inventory/Used-Vehicles
          // Inventory/Stock#1234

          currPaths.push(route.ecomMod + '/:id');
        }
      } else {
        const nonRootComponent = self.fetchNonRootComponent(currPaths);
        currComponent = require('../components/pages/' + nonRootComponent);
      }

      currPaths.map((currPath) => {
        const props = { key: currPath, path: currPath, component: currComponent };
        currRoutesState.push(<Route { ...props } />);
      });

      if (route.childNodes !== undefined) {
        self.fetchMenuSystem(route.childNodes);
      }
    });
    return currRoutesState;
  }

  fetchRoutes() {
    const result = this.fetchMenuSystem();
    return (
      <Route component={ require('../components/APP') }>
        { result }
        <Route path="SiteMap" component={ require('../components/pages/Site-Map') }/>
        <Route path="*" component={ require('../components/pages/Not-Found') }/>
      </Route>
    );
  }

  render() {
    if (this.props.items.length === 0) return <div>Loading ...</div>;
    const routerProps = {
      routes: this.fetchRoutes(),
      createElement: (component, props) => {
        return React.createElement(component, { ...props });
      }
    };
    return (
      <Router { ...routerProps } history= { this.props.history } />
    );
  }
}

Routes.propTypes = {
  clientId: PropTypes.string.isRequired,
  history: PropTypes.object.isRequired,
  items: PropTypes.array.isRequired
};

export default connectToStores(Routes);

导航项.json:

{
  "data": [
    {
      "id": 1,
      "parentId": null,
      "linkTo": "/",
      "paths": [
        "/",
        "Home"
      ],
      "title": "Home",
    },
    {
      "id": 2,
      "parentId": null,
      "linkTo": "About-Us",
      "title": "About Us",
    },
    {
      "id": 3,
      "parentId": null,
      "ecomMod": "Inventory",
      "linkTo": "Inventory",
      "title": "Inventory",
      "childNodes": [
        {
          "id": 30,
          "parentId": 3,
          "ecomMod": "Inventory",
          "linkTo": "Inventory/All-Vehicles",
          "title": "All Vehicles",
        }
        ]
    }
  ]
}
4

1 回答 1

1

解决了

将近一天后,我解决了这个问题,我犯的错误是如此愚蠢,如此明显,我不敢相信我没有看到它。

正如我所怀疑的,动态路由器很好。问题出在下拉菜单上。我怀疑这是页面上的硬编码链接有效。

举例来说,这就是 Inventory 路由的创建方式:

<Route path="Inventory" component="Inventory">
   <Route path="Inventory/All-Vehicles" component="Inventory" />
</Route>

因此,任何人都很清楚,如果大坝父级有一个路由处理程序事件,那么所有车辆的点击都会“冒泡”到它的父级,而这正是我所拥有的。

所以,就我而言,这个父链接:

  <li id={ this.props.item.id }
      ......
      onClick={ this.routeHandler.bind(this, { newLinkTo } ) } >
    <span className="">{ this.props.item.title }</span>
      // get the children
      <div>{ this.fetchSubMenu(this.props.item) }</div>
  </li>

就是现在:

  <li id={ this.props.item.id }
      ......
  >
    <Link to={ newLinkTo } className="">{ this.props.item.title }</Link>
      // get the children
      <div>{ this.fetchSubMenu(this.props.item) }</div>
  </li>

经验教训:如果您在节点树上有一个路由处理程序,它将接管孩子尝试进行的任何路由更改。

于 2015-11-05T18:28:17.197 回答