【发布时间】:2019-10-24 20:01:03
【问题描述】:
我正在使用 react router dom 版本 5.0.1 来制作一个简单的 react 应用程序,我使用 rollup 进行捆绑,这是我的路由器组件
return(
<Router>
<Switch>
<Route path='/' component={Main} />
<Route path='/hello' component={Hello} />
<Route path='/login' component={Login} />
</Switch>
</Router>
)
问题是它只在 localhost:8000/ 显示主路由,但是当我尝试访问 localhost:8000/hello 或 localhost:8000/login 时,它给了我这个错误
404 Not Found
C:\Users\omar_\Desktop\form-builder\form-builder\frontend\public\hello
(rollup-plugin-serve)
这是我的 rollup.config
import babel from "rollup-plugin-babel";
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';
import serve from 'rollup-plugin-serve'
export default {
input: 'src/index.js',
plugins: [
resolve({
browser: true,
}),
commonjs({
include: [
'node_modules/**',
],
exclude: [
'node_modules/process-es6/**',
],
namedExports: {
'node_modules/react/index.js': ['Children', 'Component', 'PropTypes', 'createElement'],
'node_modules/react-dom/index.js': ['render'],
'node_modules/react-is/index.js': ['isValidElementType'],
},
}),
babel({
exclude: "node_modules/**",
}),
replace({
'process.env.NODE_ENV': JSON.stringify('development'),
}),
serve('public')
],
output: {
file: "public/bundle.js",
format: "cjs",
sourcemap: 'inline'
}
};
【问题讨论】:
-
您是否在文件顶部导入 Hello 和 Login 组件?
-
是的,我确定我从正确的路径导入它们,我尝试更改主路径的组件@路径:“/”,它适用于所有 imorted 组件
标签: javascript reactjs react-router react-router-dom rollup