项目基本配置
dms-vue
└── config
└── webpack.config.js
└── webpack.dev.js
└── webpack.prod.js
└── public
└── index.html
└── src
└── index.js
└── utils
└── .babelrc
└── .gitignore
└── package.json
└── README.md
问题
解决方案
- 网上找到的方法:
script
标签应该放到body
中,待页面元素加载成功之后再加载js
-
实际操作:
new HtmlWebpackPlugin({ + inject: 'body', showErrors: true, hash: true, minify: { removeAttributeQuotes: true } }), inject,默认值是true,参数有 true || 'head' || 'body' || false 当设置为true或者body时,js资源将放在body的底部执行。 当设置为false或者head时,script标签对将放在head元素中 所以,即使不加这一项,js资源也是被放到body元素底部的
-
- 最终方案:配置
HtmlWebpackPlugin
1下的template
-
实际操作:
new HtmlWebpackPlugin({ - inject: 'body', + template: path.resolve(__dirname, "../public/index.html"), showErrors: true, hash: true, minify: { removeAttributeQuotes: true } }), template,指本地模板文件的位置,可以是一个绝对或者相对路径 1. 默认是使用 src/index.ejs(如果这个文件存在的话) 2. 你可以编辑自己的模板文件,然后通过 template 配置路径
-
关于模板文件(题外话)
<!--index.html-->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<div id="root"></div>
</body>
</html>
title
- title的配置
- 在
<title></title>
标签对中填写- 配置集中处理,防止项目过大配置分散,可以采用下面在
webpack
中配置
- 配置集中处理,防止项目过大配置分散,可以采用下面在
- 在
webpack.common.js
(这是本项目中的文件,文件名字可能与伙伴们的不一样)中配置,模板文件index.html
中读取,使用ejs
模板语法(<%%>)
读取,因为webpack中默认使用ejs-loader
<title><%= htmlWebpackPlugin.options.title %></title> // index.html 注意!!!是htmlWebpackPlugin,h是小写
// webpack.common.js new HtmlWebpackPlugin({ + title: 'rym' template: path.resolve(__dirname, "../public/index.html"), showErrors: true, hash: true, minify: { removeAttributeQuotes: true } }),
- 在