gpt4 book ai didi

javascript - React-router 不显示组件

转载 作者:数据小太阳 更新时间:2023-10-29 04:29:50 25 4
gpt4 key购买 nike

我目前正在学习 react-router,然后尝试在示例应用中实现它。

这是我的代码:

index.html

<!DOCTYPE html>
<html>
<head>
<title>Sample APP</title>
<link rel="stylesheet" href="dist/css/style.css">
</head>

<body>
<div id="main"></div>
</body>

<script src="dist/js/main.js"></script>
</html>

/src/app.jsx

var React = require('react');
var ReactDOM = require('react-dom');

var Routes = require('./routes');

ReactDOM.render(Routes, document.getElementById('main'));

/src/routes.jsx

var React = require('react');
var Router = require('react-router').Router;
var Route = require('react-router').Route;
var Link = require('react-router').Link;

var HelloWorld = require('./components/hello-world');
var NotFound = require('./components/not-found');
var About = require('./components/about');

module.exports = (
<Router>
<Route path="/" component={HelloWorld}>
<Route path="about" component={About}/>
</Route>
</Router>
);

/src/components/hello-world.jsx

var React = require('react');
var Link = require('react-router').Link;

module.exports = React.createClass({
render: function() {
return <div>
<h1>
Hello World !
</h1>
<Link to="/about">About</Link>
</div>
}
});

/src/components/about.jsx

var React = require('react');

module.exports = React.createClass({
render: function() {
return <h1>
About
</h1>
}
});

package.json

{...}
"dependencies": {
"browserify": "^9.0.3",
"gulp": "^3.8.11",
"gulp-concat": "^2.5.2",
"gulp-react": "^3.0.1",
"gulp-sass": "^2.0.1",
"gulp-server-livereload": "^1.3.0",
"gulp-util": "^3.0.4",
"gulp-watch": "^4.2.4",
"history": "^1.13.1",
"node-notifier": "^4.2.1",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"react-router": "^1.0.0",
"reactify": "^1.1.0",
"vinyl-source-stream": "^1.1.0",
"watchify": "^2.4.0"
}

我使用 gulpbrowserifyreactify 我的源到 /dist/js/main.js

当我点击我的 about 链接时,url 从 http://localhost:8000/#/?_k=zkmiyh 更改为 http://localhost:8000/#/about?_k=6nhkao 但是显示的组件还是hello-world

我的控制台上没有显示任何错误。

是不是漏了什么?

最佳答案

丑陋的解决方案是像这样摆脱嵌套路由:

module.exports = (
<Router>
<Route path="/" component={HelloWorld} />
<Route path="/about" component={About} />
</Router>
);

然而,我通过 this doc 找到了正确的方法

我们需要另一个组件(我称之为 Main),它将包含要显示的正确组件(即 this.props.children)。

我使用 IndexRoute 定义要显示的默认组件。

修改后的代码如下:

/src/components/main.jsx()

var React = require('react');

module.exports = React.createClass({
render: function() {
return <div>
// this is where you can add header
{this.props.children}
// this is where you can add footer
</div>
}
});

/src/routes.jsx(修改)

var React = require('react');
var Router = require('react-router').Router;
var Route = require('react-router').Route;
var IndexRoute = require('react-router').IndexRoute;

var Main = require('./components/main');
var HelloWorld = require('./components/hello-world');
var About = require('./components/about');

module.exports = (
<Router>
<Route path="/" component={Main}>
<IndexRoute component={HelloWorld} />
<Route path="about" component={About} />
</Route>
</Router>
);

关于javascript - React-router 不显示组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33891194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com