vue3.x项目启动后,自动打开浏览器地址为http://0.0.0.0:8080 无法正常访问 热更新

1,429 阅读1分钟
  1. 问题:vue3.x项目启动后,如何自动打开浏览器???配置open 可调起浏览器 地址栏为:http://0.0.0.0:8080 手动复制本地ip或localhost均可打开。这样太麻烦啦......

  2. 思路:看更新记录、找配置、看文档/(vue-cli、webpack注意版本)

  • useLocalIp等配置启动后显示Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. options has an unknown property 'useLocalIp'. These properties are valid:
  • 查看Vue-cli文档并没有详细说明image.png
  • 查看迁移说明文档(注意只有英文版)找到@vue/cli-service(We've upgraded the underlying webpack version to 5. There are plenty of breaking changes underlyingly, listed in the release announcement page Webpack 5 release (2020-10-10).) 大概意思就是vue-cli已经将底层升级到webpack 5
  • 打开Webpack 5 改动较大请认真查看具体配置
  1. 解决:
  运行环境:
  node v12.13.0
  vue 3.2.13
  @vue/cli 5.0.3
  配置文件:vue.config.js
  devServer: {
    open: true,
    host: 'local-ip', // 打开本地ip
    client: {
      // logging: 'info', // 日志
      overlay: { // 编译错误或警告时 是否全屏显示
        errors: true,
        warnings: false,
      },
      // progress: true, // 在浏览器百分比显示编译进度
      // 指定 URL 到 web socket 服务器
      // To get protocol/hostname/port from browser
      // webSocketURL: 'auto://0.0.0.0:0/ws',
      webSocketURL: {
        hostname: '0.0.0.0', // 不是真正意义上的IP 是一个本地ip的集合
        pathname: '/ws',
        port: 0, // 指定port 只在port相同时 会热更新
      },
    },
  },

notes: 当host: 'local-ip'时,特别注意webSocketServer的配置,配置有误可能导致热更新失效