vue-cli 创建的项目里 public 文件夹下的 index.html文件作用

3,133 阅读1分钟

vue-cli官方文档的解释

public/index.html 文件是一个会被 html-webpack-plugin 处理的模板。在构建过程中,资源链接会被自动注入。另外,Vue CLI 也会自动注入 resource hint (preload/prefetch、manifest 和图标链接 (当用到 PWA 插件时) 以及构建过程中处理的 JavaScript 和 CSS 文件的资源链接。

vue.config.js中的配置

module.exports = {
    chainWebpack(config) {
        config.plugin('html').tap((args) => {
            args[0].title = 'Lily';
            args[0].favicon = resolve('./public/favicon.ico');
            ...
            return args;
        });
    }
}

webpack中的配置

new HtmlWebpackPlugin({
    template: 'index.html'
})

vue-cli项目中未配置public/index.html出现的问题

1、观察build之后的dist文件,index.html的title,默认会是'Vue App',即使在项目中对document.title做了动态设定,也在页面初始加载的时候document.title为'Vue App'。

document.title默认为'Vue App'的原因

vue-cli源码中可以找到原因:packages/@vue/cli-service/lib/config中,app.js:

image.png index-default.html:

image.png