有没有大佬知道 vue-cli 创建的项目里 public 文件夹下的 index.html文件是干什么用的?

有没有大佬知道 vue-cli 创建的项目里 public 文件夹下的 index.html文件是干什么用的?
看了半天官网,就说会被 webpack 处理的模板,别的也不说。搞不懂

阅读 19.4k
2 个回答

index.html是一个总的入口文件,vue是单页面应用,挂在id为app的div下然后动态渲染路由模板

pubilc/index.html是一个模板文件,作用是生成项目的入口文件,webpack打包的js,css也会自动注入到该页面中。我们浏览器访问项目的时候就会默认打开生成好的index.html

webpack配置中是这样的:

    // generate dist index.html with correct asset hash for caching.
    // you can customize output by editing /index.html
    // see https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: config.build.index,
      template: 'index.html',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      },
      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
      chunksSortMode: 'dependency'
    }),

通过html-webpack-plugin插件读取模板(index.html),进行模板绑定之后再输出到outut目录的index.html

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题