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>
相关推荐
Zero10171313 分钟前
【详解pnpm、npm、yarn区别】
前端·react.js·前端框架
&白帝&34 分钟前
vue右键显示菜单
前端·javascript·vue.js
Wannaer34 分钟前
从 Vue3 回望 Vue2:事件总线的前世今生
前端·javascript·vue.js
羽球知道1 小时前
在Spark搭建YARN
前端·javascript·ajax
光影少年1 小时前
vue中,created和mounted两个钩子之间调用时差值受什么影响
前端·javascript·vue.js
青苔猿猿1 小时前
node版本.node版本、npm版本和pnpm版本对应
前端·npm·node.js·pnpm
Ten peaches2 小时前
Selenium-Java版(操作元素)
java·selenium·测试工具·html
一只码代码的章鱼2 小时前
Spring的 @Validate注解详细分析
前端·spring boot·算法
zimoyin2 小时前
Kotlin 协程实战:实现异步值加载委托,对值进行异步懒初始化
java·前端·kotlin
cdcdhj2 小时前
vue用通过npm的webpack打包编译,这样更适合灵活配置的项目
vue.js·webpack·npm