vue cli 中引入公共模板
vue cli 创建的项目中,在页面中引入公共 HTML 代码的方法是模板模块化。
步骤:
1. 安装 html-webpack-plugin
npm install --save-dev html-webpack-plugin
2. 配置 vue.config.js
立即学习“前端免费学习笔记(深入)”;
在 vue.config.js 中配置 html-webpack-plugin:
const fs = require('fs') const header = fs.readFileSync('./public/header.html').toString() module.exports = { chainWebpack: (config) => { config.plugin('html').tap((args) => { args[0].header = header return args }) } }
3. 创建公共模板文件
在 public 目录下创建包含要引入的 HTML 代码的文件,如 header.html:
<!DOCTYPE html> <html> <head> <!-- 公共头部代码 --> </head> <body> </body> </html>
4. 在 index.html 中引用公共模板
在 public/index.html 中引用公共模板:
<head> <!-- ... --> </head> <body> <%= htmlWebpackPlugin.options.header %> <div id="app"></div> </body>
5. 运行项目
运行 npm run serve 启动项目。此时,公共模板中的 HTML 代码将被引入到所有页面中。
以上就是如何在 Vue CLI 项目中引入公共 HTML 模板?的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号