Vue多页面

vue.config.js
官网地址:https://cli.vuejs.org/zh/config/#pages

注意事项
  1. pages 内的 entry 是每个单独的 main.js (或自定义名称),pages 下的每个属性名就是页面的 url
  2. 多页之间不能共用 vuex,考虑本地存储(说是每个页面会重新实力化一个 Vue,其中注册的 vuex/router 等会重新初始化)
  3. 单个页面使用路由时,在 main.js 内渲染函数注册的 .vue 文件内只能写<router-view />,不然在这里的东西会重复两遍
  4. router 的 mode 使用 history 时候,单独页面的总路由不能写 path: '/',只能写 path: '/login',不然显示不出来
  5. hash 下的 router 使用时候没有上述问题,path: '/'不存在上述问题
module.exports = {
  pages: {
  	index: {
  	  entry: 'path'
  	}
  }
}

const path = require('path')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const port = 8888 // dev port

module.exports = {
  publicPath: '/',  //路径
  outputDir: 'dist', 
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  pages: {
    index: {
      entry: 'src/views/index/main.js',
      template: 'public/index.html',
      filename: 'index.html',
      title: '首页',
      chunks: ['chunk-vendors', 'chunk-common', 'index']
    },
    login: {
      // page 的入口
      entry: 'src/views/login/main.js',
      // 模板来源
      template: 'public/login.html',
      // 在 dist/index.html 的输出
      filename: 'login.html',
      title: '首页',
      chunks: ['chunk-vendors', 'chunk-common', 'login']
    },
    vendor: {
      // page 的入口
      entry: 'src/views/vendor/main.js',
      // 模板来源
      template: 'public/vendor.html',
      // 在 dist/index.html 的输出
      filename: 'vendor.html',
      title: '首页',
      chunks: ['chunk-vendors', 'chunk-common', 'vendor']
    },
    merchant: {
      // page 的入口
      entry: 'src/views/merchant/main.js',
      // 模板来源
      template: 'public/merchant.html',
      // 在 dist/index.html 的输出
      filename: 'merchant.html',
      title: '首页',
      chunks: ['chunk-vendors', 'chunk-common', 'merchant']
    },
    design: {
      // page 的入口
      entry: 'src/views/design/main.js',
      // 模板来源
      template: 'public/design.html',
      // 在 dist/index.html 的输出
      filename: 'design.html',
      title: '首页',
      chunks: ['chunk-vendors', 'chunk-common', 'design']
    }
  },
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      '/api': {
        target: `https://test.yantuyunke.com`,
        secure: false,
        pathRewrite: {
          '/api': 'https://test.yantuyunke.com'
        }
      }
    }
  },
  configureWebpack: {
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  chainWebpack(config) {
    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    // set preserveWhitespace
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()

    config
    // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'development',
        config => config.devtool('cheap-source-map')
      )

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
            // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          config.optimization.runtimeChunk('single')
        }
      )
  }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值