当前位置: 首页 > 工具软件 > serve2 > 使用案例 >

vue2项目npm run serve自动打开网页为0.0.0.0

周云
2023-12-01

问题:

1、在vue2 项目中,输入命令行  npm run serve   浏览器实现自动打开功能,需要添加 项目根目录下的:在配置文件(package.json)中添加 --open

"scripts": {
    "serve": "vue-cli-service serve --open",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },

2.添加完之后,重新运行 npm run serve,

浏览器确实是自动打开了,但是访问的地址是:0.0.0.0:8080, 真正内容没有出来

原因:

vue-cl脚手架在不配置情况下,自动打开 :启动默认打开http://0.0.0.0:8080,浏览器显示无法访问

解决:

还是在去vue.config.js更新一下配置。默认情况下vue.config.js文件是没有,没有的话,在项目根目录与package.json同级,创建一个此名字的文件。

然后写一下内容:

const {defineConfig} = require('@vue/cli-service')
module.exports = defineConfig({
    transpileDependencies: true,
    runtimeCompiler: true,

    devServer: { //配置自动打开浏览器的地址
        host: 'localhost',
        port: 8080,
    },

})

那npm run server会自动打开页面是正常的了。

 类似资料: