首页 文章

将全局基本名称/上下文添加到React和npm设置

提问于
浏览
0

我的网址有问题,我需要运行我的应用程序,而不是从localhost:8081,但是从localhost:8081 / admin,所以所有页面都显示404,其中有/ ....,无论如何主页也不起作用好的,在index.html我也有标签,文件的例子 .

<link href="../public/css_admin/fonts/fonts.css" rel="stylesheet"/>

Webpack配置

var path = require('path');

module.exports = {
    entry: './src/index.jsx',
    output: {
        path: path.resolve('dist'),
        publicPath: '/admin/',
        filename: 'bundle.js'
    },
    resolve: {
        extensions: ['.js', '.json', '.jsx']
    },
    .....
    devServer: {
        contentBase: './',
        historyApiFallback: true,
        host: 'localhost',
        port: 8081
    },
    externals: {
        config: JSON.stringify({
            apiUrl: '..../rest/v1'
        })
    }
}

应用路线

render() {
        return (
            <Router history={history}  basename="admin">
                <div>                    
                    <Switch>
                        <PrivateRoute exact path="/" component={MainIndexPage} refresh={false} />
                        <PrivateRoute exact path="/staff" component={StaffPage}  refresh={false}  />
                        <Route path="/register" component={RegisterPage} />                   
                    </Switch>
                </div>
             </Router>

    );
}

2 回答

  • 0

    首先,我是初学者 . basename =“admin”或basename =“/ admin” .

  • 0

    所以请在历史记录中添加basename,

    export const history = createBrowserHistory({basename: '/admin'});
    

    也改变

    filename: 'bundle.js' -> filename: 'app-[hash].js'
    

相关问题