目前,使用Vue CLI 3生成项目后的标题为“Vue App”。
如果我通过document.title在created钩子中设置标题,则浏览器仍会在显示通过document.title设置的标题之前闪现“Vue App”。
寻找一种在Vue CLI 3生成的项目中设置HTML标题且不会先闪现默认的“Vue App”标题的方法。
您可以在 /public/index.html
中静态设置标题。
将其设置为空字符串并在 hook 中保持更新可消除闪烁。
您可以通过修改vue.config.js文件来使用自定义的index.html页面:
module.exports = {
publicPath: '/',
chainWebpack: config => {
config
.plugin("html")
.tap(args => {
args[0].template = './public/index.html'
return args
})
}
};
package.json
文件的 scripts
部分中添加 postinstall
:
"postinstall": "cp ./public/index.html ./node_modules/@vue/cli-service/lib/config/index-default.html"
<!DOCTYPE html><html lang="us"><head><title>My Title</title></head><body><div id="app"></div></body></html>
。 - zett42index.html
文件,现在需要恢复默认内容。你可以从\node_modules\@vue\cli-service\lib\config\index-default.html
获取它。它几乎完全包含了 @zett42 在上面评论中展示的内容。 - dotNETindex.html
文件在项目的根目录下。一旦我将它移动到public
目录中,Vue CLI 就会捕捉到我对<title>
的更改。 - Fearnbuster