💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 项目初始化 ## 搭建开发环境 >[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"; ``` # 总结 本节课其实还是对之前所有学习东西的复习。