当前位置:   article > 正文

搭建react多页面应用

react多页面应用

1.安装create-react-app

npm install -g create-react-app

2.创建项目

create-react-app multifypages

3.进入项目目录

cd multifypages

4.

npm start

现在react的应用页面已经可以运行起来了

5.然后进入src/App.js 用一下内容替换掉原有内容

  1. import React, { Component } from 'react';
  2. import './App.css';
  3. class App extends Component {
  4. render() {
  5. return (
  6. <div className="App">
  7. <div className="App-header">
  8. <h2>Welcome to App1</h2>
  9. </div>
  10. <p className="App-intro">
  11. To get started, edit <code>src/App.js</code> and save to reload.
  12. </p>
  13. </div>
  14. );
  15. }
  16. }
  17. export default App;

6.将 index.js 中的 内容替换为如下代码(删除registerServiceWorker)

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import './index.css';
  4. import App from './App';
  5. ReactDOM.render(<App />, document.getElementById('root'));

7.然后删除src下面的registerServiceWorker.js(该文件用于构建pwa应用用的,暂时我们用不了)和 logo.svg文件(不想处理图片文件)和 App.test.js(用于测试用的)。然后在src下建2个文件夹,将src下的App.css、App.js、index.css、index.js分别粘贴进去 并且在根目录下 新建webpack.config.js文件 目录如下

8.将以下内容 贴到webpack.config.js文件中

  1. const webpack = require('webpack');
  2. const glob = require('glob');
  3. const path = require('path');
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. const ExtractTextPlugin = require('extract-text-webpack-plugin');
  6. const CleanWebpackPlugin = require('clean-webpack-plugin');
  7. const webpackConfig = {
  8. entry: {},
  9. output:{
  10. path:path.resolve(__dirname, './dist/'),
  11. filename:'[name]/[name].[chunkhash:6].js'
  12. },
  13. //设置开发者工具的端口号,不设置则默认为8080端口
  14. devServer: {
  15. inline: true,
  16. port: 8082
  17. },
  18. module:{
  19. rules:[
  20. {
  21. test:/\.js?$/,
  22. exclude:/node_modules/,
  23. loader:'babel-loader',
  24. query:{
  25. presets:['es2015','react']
  26. }
  27. },
  28. {
  29. test: /\.(scss|sass|css)$/,
  30. loader: ExtractTextPlugin.extract({fallback: "style-loader", use: "css-loader"})
  31. },
  32. ]
  33. },
  34. plugins: [
  35. new ExtractTextPlugin("[name]/[name].[chunkhash:6].css"),
  36. new CleanWebpackPlugin(
  37. ['dist'], 
  38. {
  39. root: __dirname,         
  40. verbose: true,           
  41. dry: false           
  42. }
  43. )
  44. ],
  45. };
  46. // 获取指定路径下的入口文件
  47. function getEntries(globPath) {
  48. const files = glob.sync(globPath),
  49. entries = {};
  50. files.forEach(function(filepath) {
  51. const split = filepath.split('/');
  52. const name = split[split.length - 2];
  53. entries[name] = './' + filepath;
  54. });
  55. return entries;
  56. }
  57. const entries = getEntries('src/**/index.js');
  58. Object.keys(entries).forEach(function(name) {
  59. webpackConfig.entry[name] = entries[name];
  60. const plugin = new HtmlWebpackPlugin({
  61. filename: name + '/' + name + '.html',
  62. template: './public/index.html',
  63. inject: true,
  64. chunks: [name]
  65. });
  66. webpackConfig.plugins.push(plugin);
  67. })
  68. module.exports = webpackConfig;

9.将package.json文件中的devDependencies部分和scripts部分用下面的文件替代

  1. {
  2. "name": "recmultify",
  3. "version": "0.1.0",
  4. "private": true,
  5. "dependencies": {
  6. "react": "^16.8.6",
  7. "react-dom": "^16.8.6",
  8. "react-scripts": "2.1.8"
  9. },
  10. "devDependencies": {
  11. "babel-core": "^6.26.0",
  12. "babel-loader": "^7.1.2",
  13. "babel-preset-es2015": "^6.24.1",
  14. "babel-preset-react": "^6.24.1",
  15. "clean-webpack-plugin": "^0.1.16",
  16. "css-loader": "^0.28.7",
  17. "extract-text-webpack-plugin": "^3.0.0",
  18. "file-loader": "^1.0.0",
  19. "glob": "^7.1.2",
  20. "html-webpack-plugin": "^2.30.1",
  21. "postcss-loader": "^2.0.6",
  22. "style-loader": "^0.18.2",
  23. "url-loader": "^0.5.9",
  24. "webpack": "^3.5.6",
  25. "webpack-dev-server": "^2.8.1"
  26. },
  27. "scripts": {
  28. "start": "webpack-dev-server --open",
  29. "build": "webpack"
  30. },
  31. "eslintConfig": {
  32. "extends": "react-app"
  33. },
  34. "browserslist": [
  35. ">0.2%",
  36. "not dead",
  37. "not ie <= 11",
  38. "not op_mini all"
  39. ]
  40. }

10.进入public文件下,删除favicon.ico和 manifest.json(构建pwa用的),然后将index.html中的内容用如下内容替换

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta
  6. name="viewport"
  7. content="width=device-width, initial-scale=1, shrink-to-fit=no"
  8. />
  9. <meta name="theme-color" content="#000000" />
  10. <title>React App</title>
  11. </head>
  12. <body>
  13. <noscript>You need to enable JavaScript to run this app.</noscript>
  14. <div id="root"></div>
  15. </body>
  16. </html>

11.删除当前目录中的node_modules,重新npm install生成

12.npm run build 生成dist目录以及对应文件夹page1 page2以及内容 想要运行其中的内容 例如 打开page1文件夹 直接点击page1.html就可以访问(要是用的是HBuilder或者webstorm的话) 如果用的是vscode安装view in browser 就用Alt+B打开当前页面

13.如果自己要本地启用,可以 npm run start,然后在http://localhost:8082/page1/page1.html查看页面进行调试

如果要是运行不起来 可以访问我的github地址 克隆下来比对配置 https://github.com/create13/multify/tree/master/recmultify

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/1021375
推荐阅读
相关标签
  

闽ICP备14008679号