React 路由器在完全匹配的 URL 上不进行重定向。

16 浏览
0 Comments

React 路由器在完全匹配的 URL 上不进行重定向。

我正在构建一个使用react-router的web应用程序。当我访问url localhost:8080/user时,它能正常工作。但是当我访问localhost:8080/user/login时,它不工作,并且控制台显示unexpected token >,这是什么意思?我无法理解这个问题。

还有一件事,在这段代码中,当我更改为其他任何类时,它也不起作用。

Routes.js

import React from 'react';
import UserBase from './UserBase.js';
import Dashboard from './Dashboard.js';
import Login from './Login.js';
import { Router, Route, IndexRoute, Link, IndexLink, browserHistory } from 'react-router'
var Routes = (
    
        
        
            
            
        
    
);
module.exports = Routes;

Login.js

import React from 'react';
class Login extends React.Component{
    constructor(){
        super();
    }
    render(){
        return (
              
              
                  

Login Form

Create Account

); } } export default Login;

如果我删除'history={browserHistory}',Routes js能正常工作,这意味着我使用丑陋的url,即使用#。如果我访问http://localhost:8080/#/user/login?_k=jtorvg,它能正常工作,那么问题是什么呢?

我使用node服务器和express包为每个请求提供服务。

var app = express();
app.use('/', express.static(path.join(__dirname, 'public')));
app.get('*', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/index.html'));
});

webpack.config.js

module.exports = {
    entry: "./app/components/EntryPoint.js",
    output: {
        filename:"public/bundle.js"
    },
    module : {
        loaders: [
            {
                test: /\.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel',
                query: {
                    presets: ['react', 'es2015']
                }
            }
        ]
    }
};

图片链接:https://i.stack.imgur.com/5ZhRP.png

0
0 Comments

问题出现的原因是在index页面的bundle.js脚本路径上有一个小错误。应该将路径改为localhost:8080/user/dashboard。

只需将<script src="bundle.js" />改为<script src="/bundle.js" />即可解决问题。

0