静态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>
相关推荐
鱼毓屿御4 分钟前
如何给用户添加权限
前端·javascript·vue.js
JustHappy7 分钟前
「web extensions🛠️」有关浏览器扩展,开发前你需要知道一些......
前端·javascript·开源
何中应15 分钟前
nvm安装使用
前端·node.js·开发工具
Java新手村17 分钟前
基于 Vue 3 + Spring Boot 3 的 AI 面试辅助系统:实时语音识别 + 大模型智能回答
vue.js·人工智能·spring boot
雯0609~22 分钟前
hiprint:实现项目部署与打印3-vue版本-独立出模板设计与模板打印页面
前端·vue.js·arcgis
杜子不疼.28 分钟前
【Linux】教你在 Linux 上搭建 Web 服务器,步骤清晰无门槛
linux·服务器·前端
程序员Agions1 小时前
useMemo、useCallback、React.memo,可能真的要删了
前端·react.js
David凉宸1 小时前
Vue 3 + TS + Vite + Pinia vs Vue 2 + JS + Webpack + Vuex:对比分析
javascript·vue.js·webpack
滕青山1 小时前
Vue项目BMI计算器技术实现
前端·vue.js
子兮曰1 小时前
深入浏览器指纹:Canvas、WebGL、Audio是如何暴露你的身份的?
前端·浏览器·canvas