【发布时间】:2015-11-05 01:03:27
【问题描述】:
问题:
此动态路由器有效,除非存在涉及参数的动态链接。
具体来说:
我可以硬编码链接或在浏览器中输入:
<Link to="Inventory/All-Vehicles">All Vehicles</Link>
http://localhost:3000/Inventory/All-Vehicles
还有代码:
const { id } = this.props.params;
console.log({ id } );
控制台显示:
{id: "All-Vehicles"}
不过,带有动态链接...
< 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
片刻然后将自身重置为(页面不会重新加载)
localhost:3000/#/库存
控制台显示:
对象{id:未定义}
我根据乔丹在下面的建议重写了这个问题。
我希望它不会太长。 我使用 alt Flux 作为我的商店
routes.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);
navItems.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",
}
]
}
]
}
【问题讨论】:
-
请编辑您的问题以包含您的实际路线和组件代码。
-
动态路由器太牵扯,无法发帖。此外,当硬编码链接正常工作时,我看不到它与动态路由器的关系。我相信它与组件(Will/Did/Should)更新/安装有关。
-
您没有发布任何
component(Will/Did/Should)Update代码。如果您的路线代码特别“涉及”,那似乎更有理由发布它。 -
哇,你不是在开玩笑。我建议联系#react-router 支持频道:discord.gg/0ZcbPKXt5bYaNQ46(这是 react-router README 中的 Discord 链接。)我想你可以将他们指向这个问题并获得一些帮助。对不起,我不能做更多!
-
他们只会帮助处理报告的错误。当您发布问题时,他们有一个自动回复器,告诉您去 SO 寻求帮助。 :-(
标签: reactjs react-router react-router-component