公共配置:

    1. //公共配置
    2. const path = require("path")
    3. const HtmlWebpackPlugin = require("html-webpack-plugin")
    4. const CopyWebpackPlugin = require("copy-webpack-plugin")
    5. module.exports = {
    6. entry: {
    7. list: "./src/list/index.js",
    8. detail: "./src/detail/index.js"
    9. },
    10. output: {
    11. filename: "scripts/[name].[chunkhash:5].js"
    12. },
    13. resolve: {
    14. alias: {
    15. "@": path.resolve(__dirname, "src") //绝对路径
    16. }
    17. },
    18. stats: {
    19. modules: false,
    20. colors: true
    21. },
    22. plugins: [
    23. new HtmlWebpackPlugin({
    24. template: "./public/list.html",
    25. filename: "list.html",
    26. chunks: ["list"]
    27. }),
    28. new HtmlWebpackPlugin({
    29. template: "./public/detail.html",
    30. filename: "detail.html",
    31. chunks: ["detail"]
    32. }),
    33. new CopyWebpackPlugin([ //把css和img移到dist目录下
    34. { from: './public', to: './' }
    35. ])
    36. ]
    37. }

    开发环境:

    1. //开发环境
    2. module.exports = {
    3. mode: "development",
    4. devServer: {
    5. open: true,
    6. openPage: "list.html", //默认进入list页面
    7. proxy: {
    8. "/api": {
    9. target: "http://yuanjin.tech:5100/",
    10. changeOrigin: true
    11. }
    12. }
    13. }
    14. }

    生产环境:

    1. //生产环境
    2. const { CleanWebpackPlugin } = require("clean-webpack-plugin")
    3. module.exports = {
    4. mode: "production",
    5. plugins: [
    6. new CleanWebpackPlugin(),
    7. ]
    8. }

    基本配置:
    webpack.config:

    1. const baseConfig = require("./webpack.base")
    2. const devConfig = require("./webpack.dev")
    3. const prodConfig = require("./webpack.prod")
    4. module.exports = function (env) {
    5. if (env && env.prod) {
    6. //生产环境
    7. const config = {
    8. ...baseConfig, //两个合并一下
    9. ...prodConfig
    10. }
    11. config.plugins = [...baseConfig.plugins, ...prodConfig.plugins]
    12. return config;
    13. }
    14. else {
    15. //开发环境
    16. return {
    17. ...baseConfig,
    18. ...devConfig
    19. }
    20. }
    21. }

    areaServicejs:导出两个函数

    1. /**
    2. * 得到所有的省份
    3. */
    4. export async function getProvinces() {
    5. return await fetch("/api/local").then(resp => resp.json()) //直接解析成json数据
    6. }
    7. /**
    8. * 根据省份id得到所有的城市
    9. */
    10. export async function getCities(parentId) {
    11. const url = `/api/local?parentId=${parentId}`;
    12. return await fetch(url).then(resp => resp.json())
    13. }

    list的indexjs:

    1. import { getProvinces } from "@/util/areaService" //@对应src目录
    2. import $ from "jquery"
    3. /**
    4. * 详情页需要:
    5. * 省份的全称
    6. * 省份的id
    7. */
    8. getProvinces().then(ps => {
    9. //ps:省份的数组
    10. const ul = $(".provinces");
    11. for (const p of ps) {
    12. const li = $("<li>").appendTo(ul);
    13. const a = $("<a>").text(p.simpleName).appendTo(li);
    14. a.prop("href", `detail.html?name=${p.areaName}&id=${p.id}`)
    15. }
    16. });

    detail的index.js:

    1. import { getCities } from "@/util/areaService"
    2. import qs from "query-string"
    3. import $ from "jquery"
    4. const parsed = qs.parse(location.search);
    5. $(".title").text(parsed.name)
    6. const dl = $("dl");
    7. getCities(parsed.id).then(cs=>{
    8. for(const c of cs){
    9. $("<dd>").text(c.simpleName).appendTo(dl)
    10. }
    11. })