javascript 反应路由器不显示组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33891194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:00:48  来源:igfitidea点击:

React-router not displaying component

javascriptreactjsreact-routerreact-jsx

提问by pistou

I'm currently learning react-router, and then trying to implement it in a sample app.

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

Here is my code:

这是我的代码:

index.html

索引.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

/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

/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

/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

/src/components/about.jsx

var React = require('react');

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

package.json

包.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"
}

I use gulpto browserifyand reactifymy sources into /dist/js/main.js

我使用gulpbrowserifyreactify我的消息来源成/dist/js/main.js

When I click on my aboutlink, url changes from http://localhost:8000/#/?_k=zkmiyhto http://localhost:8000/#/about?_k=6nhkaoyet the displayed component is still hello-world.

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

There is no error displayed on my console.

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

Is there something missing out?

有什么遗漏吗?

回答by pistou

The uglysolution is to get rid of nested routeslike so:

丑陋的解决办法是摆脱嵌套的路线,像这样:

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


Yet, I figured it out the right way to dovia this doc

然而,我通过这个文档找到了正确的方法

We need another component (which I called Main) that will contain the right component to display (i.e. this.props.children).

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

I use IndexRouteto define the defaultcomponent to display.

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

Here is the corrected code:

这是更正后的代码:

/src/components/main.jsx (new)

/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 (modified)

/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>
);

回答by Dhaval Patel

I have also same prob in my APP I have changed parent and set as same node then it's working may be it may help you

我在我的 APP 中也有同样的问题我已经改变了父节点并设置为相同的节点然后它正在工作可能会帮助你

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

);

);