# 项目初始化
## 搭建开发环境
>[danger] 这里需要打开笔记中的复习资料webpack
> webpack.config.js
```
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry:path.join(__dirname,'./src/main.js'),
output: {
path: path.join(__dirname,'./dist/'),
filename: "bundle.js"
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname,'./src/index.html'),
filename: "index.html"
})
],
module: {
rules: [
{test:/\.css$/, use: ['style-loader','css-loader?modules&localIdentName=[name]__[local]--[hash:5]']},
{test:/\.scss$/, use: ['style-loader','css-loader','sass-loader']},
{test:/\.less$/, use: ['style-loader','css-loader','less-loader']},
{test:/\.(png|jpg|jpeg|gif)$/, use: ['url-loader']},
{test:/\.jsx?$/, use: ['babel-loader'], exclude: path.join(__dirname,'./node_modules')},
]
}
}
```
## 安装React依赖
`yarn add react react-dom react-router-dom`
支持jsx
`yarn add babel-preset-react`
配置.babelrc
```
{
"presets": ["env","stage-0","react"],
"plugins": ["transform-runtime"]
}
```
## 实现路由环境
### 修改main.js
```
import React from "react";
import ReactDOM from 'react-dom'
import {HashRouter, Route} from "react-router-dom";
import App from "./App";
ReactDOM.render(
<HashRouter>
<Route path={'/'} component={App}></Route>
</HashRouter>
, document.getElementById('app'))
```
### 修改app.js
```
import React, {Component, Fragment} from 'react';
import {HashRouter, Link, Redirect, Route, Switch} from "react-router-dom";
import Home from "./Pages/Home";
import Movie from "/Pages/Movie";
import About from "./Pages/About";
export default class App extends Component {
constructor(props) {
super(props);
this.state = {}
}
render() {
return (
<Fragment>
<Link to={'/home'}>首页</Link>
<Link to={'/movie'}>电影</Link>
<Link to={'/about'}>详情</Link>
<hr/>
<Switch>
<Route path={'/home'} component={Home}/>
<Route path={'/movie'} component={Movie}/>
<Route path={'/about'} component={About}/>
<Redirect to={'/home'} />
</Switch>
</Fragment>
);
}
}
```
### 路由懒加载
webpack配置
```
{test:/\.jsx?$/, use: ['babel-loader','lazyload-loader'], exclude: path.join(__dirname,'./node_modules')},
```
项目中使用
```
import Home from "./Pages/Home";
import Movie from "lazy!./Pages/Movie";
import About from "lazy!./Pages/About";
```
# 总结
本节课其实还是对之前所有学习东西的复习。
- webpack复习
- React基础
- 前端三大主流框架对比
- React中几个核心的概念
- React基础语法
- React JSX语法
- React组件
- 普通组件
- 组件通信-父向子传递
- 组件拆成单个文件
- 面向对象复习
- Class组件基础
- Class组件的私有状态(私有数据)
- 案例:实现评论列表组件
- 组件样式管理
- 组件样式分离-样式表
- CSS模块化
- 生命周期
- React组件生命周期
- Counter组件来学习组件生命周期
- 生命周期总结
- 生命周期案例
- React评论列表
- React双向数据绑定
- React版todolist
- 其它提高(了解)
- 组件默认值和数据类型验证
- 绑定this并传参的三种方式
- 祖孙级和非父子组件传递数据(了解)
- React路由
- 路由基础
- 动态路由
- 路由严格模式
- 路由导航定位
- 路由重定向
- 路由懒加载
- WolfMovie项目
- 项目初始化
- AntDesign使用
- 其它相关了解