问题:
此动态路由器有效,除非存在涉及参数的动态链接。
具体来说:
我可以硬编码链接或在浏览器中输入:
<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",
}
]
}
]
}