React-router不显示组件

时间:2015-11-24 10:28:58

标签: javascript reactjs react-router react-jsx

我目前正在学习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"
}

我将gulp用于browserifyreactify我的来源/dist/js/main.js

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

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

是否有遗漏的东西?

3 个答案:

答案 0 :(得分:13)

丑陋的解决方案是摆脱嵌套路线,如下所示:

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

然而,我通过this doc找出正确的方式

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

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

以下是更正后的代码:

/src/components/main.jsx( new

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

答案 1 :(得分:1)

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

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

);

答案 2 :(得分:-1)

//this is the router jump to   = DetailedView
import {router} from "@/router/index";
export default defineComponent({
        name: "DetailedView",
        props: {data: String},
        setup(props, { attrs, slots, emit }){            
  
         let { d }= toRefs(props)
         console.log("d is:", d.value);  //error message populate here
        
         let m = props.data;
         console.log("m is:", m);  //OK. But it will remove props reactivity.

这可能会帮助您检查一下。