静态HTML引入vue封装组件

在对历史原生html代码使用vue重构项目时,可以利用vue相关组件进行项目优化、实现统一管理,本文结束html+vue的构建方式,欢迎大家阅读交流。

1、 下载vue.js

可自行到官网下载所需版本或者使用cdn资源

2、封装通用组件

2.1 封装通用实例化main.js

javascript 复制代码
import { createApp} from '/vue.esm-browser.js'
import commonHead from '/common/Head.js'
import commonFooter from '/common/Footer.js'

function init(param) {
    param.created = function () {
        const css = [
            'https://unpkg.com/element-ui/lib/theme-chalk/index.css',
            'themes/home.css',
            'assets/css/index.css'
        ]
        const scripts = [
            'https://unpkg.com/element-ui/lib/index.js'
        ]
        for (const href of css) {
            console.log(href)
            let style = document.createElement('link');
            style.rel = "stylesheet";
            style.href = href;
            document.head.append(style)
        }
        for (const src of scripts) {
            let script = document.createElement('script');
            script.src = src;
            document.head.append(script)
        }

    }
    const app = createApp(param)
    app.component('common-head', commonHead);
    app.component('common-footer', commonFooter);
    app.mount("#app");
}
export {init}

2.2、封装组件 Head.js /Footer.js

2.2.1 Head.js

javascript 复制代码
let head=`
    <el-header style="height: 10%;">
        <div class="header">
            我是Header
            </div>
        </div>
    </el-header>
    `
export default {
    template:head ,
}
 

2.2.2 Footer.js

javascript 复制代码
let footer=`
    <el-header style="height: 10%;">
        <div class="header">
            我是Footer
            </div>
        </div>
    </el-header>
    `
export default {
    template:footer,
}
 

3、在html中进行实例化

javascript 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<div id="app">
    <common-head></common-head>
    <div class="container">
        <h1>{{ message }}</h1>
        <input type="text" v-model="name"></input>
        <h1>{{ name }}</h1>
        <button @click="getData">get</button>
    </div>
    <common-footer></common-footer>
</div>
</body>
<script type="module" >
    import {init} from '/main.js'
    init({
        data() {
            return {
                message: 'Hello World!',
                name:'name',
                userName:''

            }
        },
        methods:{
            getData:function(){
                alert(this.name);
            }
        }
    })
</script>
<style>
    #app{
        padding: 0px;
        margin: 0px;
        height: 100%;
        width: 100%;
    }
    .container{
        background-color: white;
        height: 55%;
        padding: 10px;
    }

</style>
</html>
相关推荐
sakiko_4 分钟前
Swift/UIkit学习笔记27-模块管理,发送位置信息
前端·笔记·学习·ios·swift·uikit
hhb_6184 分钟前
Ruby核心技术难点梳理与实战应用案例解析
服务器·前端·ruby
天渺工作室13 分钟前
Vue自定义指令实现点击事件权限拦截控制的npm插件
前端·vue.js·npm
晓得迷路了14 分钟前
栗子前端技术周刊第 129 期 - TanStack npm 供应链入侵事件、pnpm 11.1、Tailwind CSS 4.3...
前端·javascript·css
Lan_Se_Tian_Ma16 分钟前
使用Cursor封装Flutter项目基建框架
前端·人工智能·flutter
ZC跨境爬虫16 分钟前
跟着 MDN 学 HTML day_59:HTML表单与按钮——构建用户交互的基石
前端·javascript·ui·html·交互·媒体
天天开发17 分钟前
Flutter Widget Previewer使用指南:提升开发效率的利器
前端·javascript·flutter
许彰午18 分钟前
IE11富文本兼容——政务系统前端的深渊
前端·政务
luck_bor22 分钟前
File 类核心笔记
java·前端·算法
ZC跨境爬虫22 分钟前
模块化烹饪小程序开发日记 Day2:全局配置与 tabBar 实现
java·前端·javascript·微信小程序·html·notepad++