html页面整合vue2或vue3

Vue 2 使用指南

1. 引入 Vue 2

1.1 下载 Vue 2
  • 下载地址 : Vue 2 下载
  • 文件名 : vue.min.js
  • 保存位置 : 项目目录/js/vue.min.js
1.2 引入 Vue 2 文件

在 HTML 文件的 <head> 部分引入 Vue 2:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue.js 2 示例</title>
    <script src="js/vue.min.js"></script>
</head>
<body>
    <!-- Vue 应用容器 -->
</body>
</html>

2. 创建 Vue 2 应用

2.1 创建 Vue 应用的容器

<body> 中添加一个 <div>,作为 Vue 应用的根元素:

html 复制代码
<body>
    <div id="app">
        <!-- Vue 应用内容 -->
    </div>
</body>
2.2 初始化 Vue 实例
html 复制代码
<script>
    new Vue({
        el: '#app',
        data: {
            message: 'Hello, Vue!'
        }
    });
</script>

3. Vue 2 实例选项

3.1 基本选项
  • el: 绑定的 DOM 元素。
  • data: 定义 Vue 实例的数据。
  • methods: 定义 Vue 实例的方法。
  • created: 生命周期钩子函数,在 Vue 实例创建后执行。
3.2 示例
html 复制代码
<script>
    new Vue({
        el: '#app',
        data: {
            title: '欢迎使用 Vue.js!',
            message: 'Hello, Vue!'
        },
        methods: {
            reverseMessage() {
                this.message = this.message.split('').reverse().join('');
            }
        },
        created() {
            console.log('Vue 实例已创建!');
        }
    });
</script>

4. 数据绑定与事件处理

4.1 数据绑定

使用双大括号 {``{ }} 进行数据绑定:

html 复制代码
<p>{{ message }}</p>
4.2 事件处理

使用 @v-on 的缩写)绑定事件处理方法:

html 复制代码
<button @click="reverseMessage">反转消息</button>

5. 完整示例

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue.js 2 示例</title>
    <script src="js/vue.min.js"></script>
</head>
<body>
    <div id="app">
        <h1>{{ title }}</h1>
        <p>{{ message }}</p>
        <input v-model="message" placeholder="编辑我">
        <button @click="reverseMessage">反转消息</button>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                title: '欢迎使用 Vue.js!',
                message: 'Hello, Vue!'
            },
            methods: {
                reverseMessage() {
                    this.message = this.message.split('').reverse().join('');
                }
            },
            created() {
                console.log('Vue 实例已创建!');
            }
        });
    </script>
</body>
</html>

Vue 3 使用指南

1. 引入 Vue 3

1.1 下载 Vue 3
  • 下载地址 : Vue 3 下载
  • 文件名 : vue.global.prod.js
  • 保存位置 : 项目目录/js/vue.global.prod.js
1.2 引入 Vue 3 文件

在 HTML 文件的 <head> 部分引入 Vue 3:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue.js 3 示例</title>
    <script src="js/vue.global.prod.js"></script>
</head>
<body>
    <!-- Vue 应用容器 -->
</body>
</html>

2. 创建 Vue 3 应用

2.1 创建 Vue 应用的容器

<body> 中添加一个 <div>,作为 Vue 应用的根元素:

html 复制代码
<body>
    <div id="app">
        <!-- Vue 应用内容 -->
    </div>
</body>
2.2 初始化 Vue 应用
html 复制代码
<script>
    const { createApp } = Vue;

    createApp({
        data() {
            return {
                message: 'Hello, Vue 3!'
            };
        }
    }).mount('#app');
</script>

3. Vue 3 实例选项

3.1 基本选项
  • mount: 挂载的 DOM 元素。
  • data: 定义 Vue 应用的数据。
  • methods: 定义 Vue 应用的方法。
  • mounted: 生命周期钩子函数,在 Vue 应用挂载后执行。
3.2 示例
html 复制代码
<script>
    const { createApp } = Vue;

    createApp({
        data() {
            return {
                title: '欢迎使用 Vue 3!',
                message: 'Hello, Vue 3!'
            };
        },
        methods: {
            reverseMessage() {
                this.message = this.message.split('').reverse().join('');
            }
        },
        mounted() {
            console.log('Vue 实例已挂载!');
        }
    }).mount('#app');
</script>

4. 数据绑定与事件处理

4.1 数据绑定

使用双大括号 {``{ }} 进行数据绑定:

html 复制代码
<p>{{ message }}</p>
4.2 事件处理

使用 @v-on 的缩写)绑定事件处理方法:

html 复制代码
<button @click="reverseMessage">反转消息</button>

5. 完整示例

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue.js 3 示例</title>
    <script src="js/vue.global.prod.js"></script>
</head>
<body>
    <div id="app">
        <h1>{{ title }}</h1>
        <p>{{ message }}</p>
        <input v-model="message" placeholder="编辑我">
        <button @click="reverseMessage">反转消息</button>
    </div>
    <script>
        const { createApp } = Vue;

        createApp({
            data() {
                return {
                    title: '欢迎使用 Vue 3!',
                    message: 'Hello, Vue 3!'
                };
            },
            methods: {
                reverseMessage() {
                    this.message = this.message.split('').reverse().join('');
                }
            },
            mounted() {
                console.log('Vue 实例已挂载!');
            }
        }).mount('#app');
    </script>
</body>
</html>
相关推荐
Data_Journal3 分钟前
Puppeteer指纹识别指南:循序渐进,简单易学!
服务器·前端·人工智能·物联网·媒体
晓得迷路了13 分钟前
栗子前端技术周刊第 128 期 - Rolldown 1.0、Vitest、Node.js 26.0.0...
前端·javascript·css
金玉满堂@bj16 分钟前
Gin 框架零基础全套入门教程(Go 企业级 Web 开发)
前端·golang·gin
qingy_204617 分钟前
浏览器页面出现竖向滚动条的解决方案
前端·javascript·vue.js
前端小万19 分钟前
用 AI 写了个 VSCode 摸鱼插件,从开发到上架全过程
vue.js
蜡台19 分钟前
Vue3 + ECharts 实现地图显示,深蓝色科技风地图、涟漪点、向上连线 ,标签
vue.js·科技·echarts·map·地图
之歆23 分钟前
DAY_17深度博客:CSS 响应式布局 · BFC · JavaScript 完全指南(下)
前端·javascript·css
光影少年26 分钟前
React18 函数组件执行顺序、严格模式下重复执行问题
前端·javascript·react.js
之歆30 分钟前
DAY_20JavaScript 条件语句与循环结构深度学习(一)
前端·javascript
lihaozecq31 分钟前
从零实现一个 ReAct Agent Loop - 可中断、可流式、多模型支持
前端·agent·ai编程