zoukankan      html  css  js  c++  java
  • 666 vue3初体验,调试Vue的源码

    学习vue2还是vue3?


    目前需要学习Vue3吗?


    Vue3带来的变化(源码)


    Vue3带来的变化(性能)

    补充:Vue 的响应式原理中 Object.defineProperty 有什么缺陷?为什么在 Vue3.0 采用了 Proxy,抛弃了 Object.defineProperty?
    1、Object.defineProperty无法监控到数组下标的变化,导致通过数组下标添加元素,不能实时响应;
    2、Object.defineProperty只能劫持对象的属性,从而需要对每个对象、每个属性进行遍历,如果属性值是对象,还需要深度遍历。Proxy可以劫持整个对象,并返回一个新的对象。
    3、Proxy不仅可以代理对象,还可以代理数组,还可以代理动态增加的属性。
    


    Vue3带来的变化(新的API)


    如何使用Vue呢?


    方式一:CDN引入


    方式一:CDN引入


    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    <body>
      
      <div id="app">
        <h2>Hello World</h2>
      </div>
    
      <script src="https://unpkg.com/vue@next"></script>
      <script>
        const why = {
          template: '<h2>Hello World</h2>'
        }
    
        const app = Vue.createApp(why);
        app.mount("#app")
      </script>
    
    </body>
    </html>
    

    方式二:下载和引入

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    
    <body>
    
      <div id="app"></div>
    
      <script src="../js/vue.js"></script>
      <script>
        Vue.createApp({
          template: `<h2>你好啊, 哈哈</h2>`
        }).mount("#app");
      </script>
    </body>
    
    </html>
    

    计数器案例


    计数器原生实现

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=edge" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Document</title>
    </head>
    
    <body>
      <h2 class="counter">0</h2>
      <button class="increment">+1</button>
      <button class="decrement">-1</button>
    
      <script>
        // 1.获取所有的元素
        const counterEl = document.querySelector(".counter");
        const incrementEl = document.querySelector(".increment");
        const decrementEl = document.querySelector(".decrement");
    
        // 2.定义变量
        let counter = 100;
        counterEl.innerHTML = counter;
    
        // 3.监听按钮的点击
        incrementEl.addEventListener("click", () => {
          counter += 1;
          counterEl.innerHTML = counter;
        });
        decrementEl.addEventListener("click", () => {
          counter -= 1;
          counterEl.innerHTML = counter;
        });
      </script>
    </body>
    
    </html>
    

    计数器Vue实现

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    
    <body>
    
      <div id="app">哈哈哈哈啊</div>
    
      <script src="../js/vue.js"></script>
      <script>
        Vue.createApp({
          template: `
            <div>
              <h2>{{message}}</h2>
              <h2>{{counter}}</h2>
              <button @click='increment'>+1</button>
              <button @click='decrement'>-1</button>
            </div>
          `,
          data: function () {
            return {
              message: "Hello World",
              counter: 100
            }
          },
          // 定义各种各样的方法
          methods: {
            increment() {
              console.log("点击了+1");
              this.counter++;
            },
            decrement() {
              console.log("点击了-1");
              this.counter--;
            }
          }
        }).mount('#app');
      </script>
    </body>
    
    </html>
    

    声明式和命令式


    MVVM模型


    template属性


    template写法


    data属性


    methods属性


    其他属性


    05_template写法一.html

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    
    <body>
      <div id="app">哈哈哈哈</div>
    
      <script type="x-template" id="why">
        <div>
          <h2>{{message}}</h2>
          <h2>{{counter}}</h2>
          <button @click='increment'>+1</button>
          <button @click='decrement'>-1</button>
        </div>
      </script>
    
      <script src="../js/vue.js"></script>
      <script>
        document.querySelector("#why")
        Vue.createApp({
          template: '#why',
          data: function () {
            return {
              message: "Hello World",
              counter: 100
            }
          },
          // 定义各种各样的方法
          methods: {
            increment() {
              console.log("点击了+1");
              this.counter++;
            },
            decrement() {
              console.log("点击了-1");
              this.counter--;
            }
          }
        }).mount('#app');
      </script>
    </body>
    
    </html>
    

    06_template写法二.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
    </head>
    <body>
      
      <div id="app"></div>
    
      <template id="why">
        <div>
          <h2>{{message}}</h2>
          <h2>{{counter}}</h2>
          <button @click='increment'>+1</button>
          <button @click='decrement'>-1</button>
          <button @click="btnClick">按钮</button>
          <button @click="btn2Click">按钮</button>
        </div>
      </template>
    
      <script src="../js/vue.js"></script>
      <script>
        document.querySelector("#why")
        const vm = Vue.createApp({
          template: '#why',
          data: function() {
            return {
              message: "Hello World",
              counter: 100
            }
          },
          // 定义各种各样的方法
          methods: {
            increment() {
              console.log("点击了+1");
              this.counter++;
            },
            decrement() {
              console.log("点击了-1");
              this.counter--;
            },
            btnClick: () => {
              // this === window? 不可以
              // 写成一个箭头函数时, 这个this就是window
              // 在箭头函数中是不绑定this, 但是函数中如果使用了this
              console.log(this);
            },
            btn2Click: function() {
              // this === window? 不可以
              // 写成一个箭头函数时, 这个this就是window
              // 在箭头函数中是不绑定this, 但是函数中如果使用了this
              console.log(this);
              console.log(this === vm);
            }
          }
        }).mount('#app');
    
      </script>
    </body>
    </html>
    

    调试Vue的源码

    使用Git下载指定分支命令为:git clone -b 分支名仓库地址
    

      使用Git下载v.2.8.1分支代码,使用命令:git clone -b v2.8.1 https://git.oschina.net/oschina/android-app.git
      如下图所示:

      解释一下这个命令:-b表示要从分支下载,v2.8.1就是具体的某个分支的名称,https://git.oschina.net/oschina/android-app.git这是app源码的git仓库地址




  • 相关阅读:
    hdu2060
    hdu1003
    style属性
    变量与常量
    使用BIgDecimal进行浮点数的精确计算
    CSUST 玩游戏 题解(思维+优先队列维护第k大)
    百度之星 迷失 题解(矩阵快速幂+分层图)
    CSUST 简单数学题 题解(质因子分解+并查集)
    CSUST 神秘群岛 题解(LCA)
    CSUST lh的简单图论 题解(图转树LCA问题)
  • 原文地址:https://www.cnblogs.com/jianjie/p/14764713.html
Copyright © 2011-2022 走看看