因为我是webpack的新手,所以我尝试采用现有的应用程序。所以我设法取得了一些结果,但是我想默认设置(通过启动应用程序)特定页面。 (实际上是主页)。因为我使用的是没有顶部导航的路由,而且必须显示的第一页也必须是首页。
webpack.config.js
const webpack = require('webpack');
module.exports = {
entry: './src/index.js',
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader']
} , {
test: /\.css$/,
use: [
{ loader: "style-loader" },
{ loader: "css-loader" }
]
} , {
test: /\.(jpe?g|png|gif|woff|woff2|eot|ttf|svg)(\?[a-z0-9=.]+)?$/,
loader: 'url-loader?limit=100000' }
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
output: {
path: __dirname + '/dist',
publicPath: '/',
filename: 'bundle.js'
},
plugins: [
new webpack.HotModuleReplacementPlugin()
],
devServer: {
contentBase: './dist',
hot: true,
package.json
{
"name": "webpack-routing-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --config ./webpack.config.js --mode development",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack --mode development",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"react-hot-loader": "^4.8.4",
"webpack": "^4.30.0",
"webpack-cli": "^3.3.1",
"webpack-dev-server": "^3.3.1"
},
"dependencies": {
"css-loader": "^2.1.1",
"file-loader": "^3.0.1",
"moment-timezone": "^0.5.25",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-moment": "^0.9.2",
"react-router-dom": "^5.0.0",
"style-loader": "^0.23.1",
"url-loader": "^1.1.2"
}
}
App.js
render(){
return (
<Switch>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/topics">Topics</Link>
</li>
</ul>
<div className='App'>
<div className='mainContainer'>
<Route exact path="/" component={Home} />
<Route exact path="/home" component={Home} />
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />
</div>
</div>
</div>
</Switch>
也许我做错了事,但是请不要对我不好。正如我上面提到的,我是webpack的新手,所以...如果有人给我一个好的答案,我将感到满意。谢谢。