Vue3 | 基础语法 与 生命周期知识及案例详解

本文详细介绍了Vue3中的核心概念,包括`createApp()`用于创建应用实例,`.mount()`指定挂载元素,以及MVVM架构中的数据和模板角色。通过示例展示了如何使用Vue3创建组件并进行数据绑定。同时,深入探讨了Vue组件的生命周期,从`beforeCreate`到`unmounted`各个阶段的行为和变化,揭示了Vue如何管理组件的状态和更新过程。
摘要由CSDN通过智能技术生成

完整原文地址见简书https://www.jianshu.com/p/8fa37a3c8e6e


createApp()、mount()、MVVM、根组件实例

-【createApp()】Vue.createApp(),创建Vue应用实例,开始使用Vue;
-【mount()】.mount()指定在哪个组件上使用Vue
(这个在《Vue3 | 基本特性概念 与语法的 应用与案例》中已经讲过了);
-传入createApp()的参数,描述了这个Vue应用实例的展示内容,包括数据、UI、各种事件等;
-【MVVM】
我们说Vue架构使用了MVVM的设计思想,其中data()是M层,template是V层,
VM层视图数据连接层,由Vue组件帮我们完成;
-【Vue应用实例根组件实例】
以下代码中,const vm = heheApp.mount('#root_div');这里,vm接收的组件实例,就是这个Vue应用实例根组件
我们可以使用这个根组件实例访问到组件内相关的 数据(data)等字段(详见下面案例):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World! heheheheheheda</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>
<body>
    <div id="root_div"></div>
</body>

<script>
    const heheApp = Vue.createApp({
        data(){
            return{
                divText:'hehehedadada'
            }
        },
        template:`
        <div>{{divText}}</div>
        `
    });

    const vm = heheApp.mount('#root_div');
</script>
</html>

运行效果:

可以打开控制台进行终端操作:

输入刚刚案例代码中的Vue实例(heheApp),查看实例内容:

查看根组件实例vm及其内容:
查看vm的字段:
查看vm的data字段:
对data中的数据字段赋值,
触发Vue的ViewModel层效果,UI数据双向绑定,
使得绑定这个数据字段对应的UI发生改变:
再尝试:

生命周期

先贴上案例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World! heheheheheheda</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>
<body>
    <div id="heheApp">
      <h1>{{heheString + ' ———— string in outer HTML'}}</h1>
    </div>
</body>
  <script>
    var vm = Vue.createApp({
      el:'#heheApp',
      data() {
        return{
            heheString: 'Vue的生命周期'
        }
      },
      beforeCreate: function() {
        console.group('------beforeCreate创建前状态------');
        console.log("%c%s", "color:red" , "el     : " + this.$el); //undefined
        console.log("%c%s", "color:red","data   : " + this.$data); //undefined 
        console.log("%c%s", "color:red","heheString: " + this.heheString) 
      },
      created: function() {
        console.group('------created创建完毕状态------');
        console.log("%c%s", "color:red","el     : " + this.$el); //undefined
        console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化 
        console.log("%c%s", "color:red","heheString: " + this.heheString); //已被初始化
      },
      beforeMount: function() {
        console.group('------beforeMount挂载前状态------');
        console.log("%c%s", "color:red","el     : " + (this.$el)); //已被初始化
        console.log(this.$el);
        console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化  
        console.log("%c%s", "color:red","heheString: " + this.heheString); //已被初始化  
        console.log("%c%s", "color:red", 
            document.getElementById('heheApp').innerHTML, " ———— beforeMount");
      },
      mounted: function() {
        console.group('------mounted 挂载结束状态------');
        console.log("%c%s", "color:red","el     : " + this.$el); //已被初始化
        console.log(this.$el);    
        console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
        console.log("%c%s", "color:red","heheString: " + this.heheString); //已被初始化 
        console.log("%c%s", "color:red", 
            document.getElementById('heheApp').innerHTML, " ———— mounted");
      },
      beforeUpdate: function () {
        console.group('beforeUpdate 更新前状态===============》');
        console.log("%c%s", "color:red","el     : " + this.$el);
        console.log(this.$el);   
        console.log("%c%s", "color:red","data   : " + this.$data); 
        console.log("%c%s", "color:red","heheString: " + this.heheString); 
        console.log("%c%s", "color:red", 
            document.getElementById('heheApp').innerHTML, " ———— beforeUpdate");
      },
      updated: function () {
        console.group('updated 更新完成状态===============》');
        console.log("%c%s", "color:red","el     : " + this.$el);
        console.log(this.$el); 
        console.log("%c%s", "color:red","data   : " + this.$data); 
        console.log("%c%s", "color:red","heheString: " + this.heheString); 
        console.log("%c%s", "color:red", 
            document.getElementById('heheApp').innerHTML, " ———— updated");
      },
      beforeUnmount: function () {
        console.group('beforeDestroy 销毁前状态===============》');
        console.log("%c%s", "color:red","el     : " + this.$el);
        console.log(this.$el);    
        console.log("%c%s", "color:red","data   : " + this.$data); 
        console.log("%c%s", "color:red","heheString: " + this.heheString); 
        console.log("%c%s", "color:red", 
            document.getElementById('heheApp').innerHTML, " ———— beforeUnmount");
      },
      unmounted: function () {
        console.group('destroyed 销毁完成状态===============》');
        console.log("%c%s", "color:red","el     : " + this.$el);
        console.log(this.$el);  
        console.log("%c%s", "color:red","data   : " + this.$data); 
        console.log("%c%s", "color:red","heheString: " + this.heheString);
        console.log("%c%s", "color:red", 
            document.getElementById('heheApp').innerHTML, " ———— unmounted");
      },
      template: "<h1>{{this.heheString +' ———— string in template'}}</h1>"
    })
    const vmR = vm.mount('#heheApp');
  </script>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌川江雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值