
vue cli template 中引入公共模板
使用 vue cli 生成的项目中,可以在图示区域引入外部包含 HTML 的文件,从而提取多页面之间的公共代码。以下是具体配置方法:
vue.config.js
const fs = require('fs');
const header = fs.readFileSync('./public/header.html').toString();
module.exports = {
runtimeCompiler: true,
css: {
loaderOptions: {
less: {
lessOptions: {
javascriptEnabled: true
}
}
}
},
chainWebpack: config => {
config.plugin('html').tap(args => {
args[0].header = header;
return args;
});
}
};public/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,minimumscale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title>test</title>
<script>
</script>
</head>
<body>
<%= htmlWebpackPlugin.options.header %>
<div id="app"></div>
</body>
</html>这样,就可以在 Vue.js 组件中引入外部 HTML 文件作为公共模板,实现代码复用。
立即学习“前端免费学习笔记(深入)”;
以上就是如何使用 Vue CLI 在多个页面中引入公共模板?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号