主頁 > 企業開發 > Vue 核心技術

Vue 核心技術

2022-10-20 07:15:15 企業開發

1.1 Vue簡介

1.1.1 官網

  • 英文官網
  • 中文官網

1.1.2 介紹與描述

  1. 動態構建用戶界面的 漸進式 JavaScript 框架
  2. 作者:尤雨溪

1.1.3 Vue的特點

  • 遵循 MVVM 模式
  • 編碼簡潔,體積小,運行效率高,適合移動/PC端開發
  • 它本身只關注UI,也可以引入其它第三方庫開發專案

1.1.4 與其它JS框架的關聯

  1. 借鑒 Angular模板和資料系結技術
  2. 借鑒 React組件化和虛擬 DOM 技術

1.1.5 Vue 周邊庫

  1. Vue CLI: 專案腳手架
  2. Vue Resource
  3. Axios
  4. Vue Router: 路由
  5. Vuex: 狀態管理
  6. element-ui:基于 VueUI 組件庫(PC端)
    ......

1.2 初識 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>初識Vue</title>
    <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
</head>
<body>
    <!-- 
        初識Vue:
            1.想讓 Vue作業,就必須創建一個 Vue實體,且要傳入一個配置物件;
            2.root 容器里的代碼依然符合 html 規范,只不過混入了一些特殊的 Vue語法;
            3.root 容器里的代碼被稱為【Vue模板】;
            4.Vue 實體和容器是一一對應的;
            5.真實開發中只有一個 Vue 實體,并且會配合著組件一起使用;
            6.{{xxx}} 中的 xxx 要寫 js 運算式,且 xxx 可以自動讀取到 data 中的所有屬性;
            7.一旦 data 中的資料發生改變,那么頁面中用到該資料的地方也會自動更新;

            注意區分:js 運算式和 js 代碼(陳述句)
                1.運算式:一個運算式會產生一個值,可以放在任何一個需要值的地方:
                    (1). a
                    (2). a+b
                    (3). demo(1)
                    (4). x === y ? 'a' : 'b'

                2.js代碼(陳述句)
                    (1). if(){}
                    (2). for(){}
    -->
    <div id="root">
        <h1>Hello {{name}}</h1>
    </div>

    <script>
        // 阻止 Vue 在啟動時生成生產提示
        // You are running Vue in development mode.
        // Make sure to turn on production mode when deploying for production.
        // See more tips at https://vuejs.org/guide/deployment.html
        Vue.config.productionTip = false

        // 創建 Vue 實體
        new Vue({
            // el 用于指定當前 Vue 實體為哪個容器服務,值通常為 css 選擇器字串
            el: '#root', 
            // data 用于存盤資料,資料供 el 所指定的容器去使用
            data: {
                name: "張三"
            },
        })
    </script>
</body>
</html>

1.3 模板語法

1.3.1 模板的理解

html 中包含了一些 js 語法代碼,語法分為兩種,分別為:

  1. 插值語法(雙大括號運算式)
  2. 指令(以v-開頭)

1.3.2 插值語法

  1. 功能:用于決議標簽體內容

1.3.3 指令語法

  1. 功能:決議標簽屬性、決議標簽體內容、系結事件
  2. 舉例:v-bind:href='https://www.cnblogs.com/codechen8848/archive/2022/10/19/xxxx'xxxx 會作為 js 運算式被決議
  3. 說明:Vue 中有有很多的指令,此處只是用 v-bind 舉個例子

1.3.4 示例代碼

<!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>Vue 模板語法</title>
    <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
</head>
<body>
    <!-- 
        Vue模板語法有2大類:
            1.插值語法:
                功能:用于決議標簽體內容,
                寫法:{{xxx}},xxx 是 js 運算式,且可以直接讀取到 data 中的所有屬性,
            2.指令語法:
                功能:用于決議標簽(包括:標簽屬性、標簽體內容、系結事件.....),
                舉例:v-bind:href="https://www.cnblogs.com/codechen8848/archive/2022/10/19/xxx" 或  簡寫為 :href="https://www.cnblogs.com/codechen8848/archive/2022/10/19/xxx",xxx同樣要寫js運算式,且可以直接讀取到 data 中的所有屬性,
                備注:Vue 中有很多的指令,且形式都是:v-????,此處我們只是拿 v-bind 舉個例子,
    -->
    <div id="root">
        <h1>插值語法</h1>
        <h3>你好,{{name}}</h3>
        <hr></hr>
        <h1>指令語法</h1>
        <a href="http://baidu.com">去百度</a>
        <br/>
        <a v-bind:href="https://www.cnblogs.com/codechen8848/archive/2022/10/19/school.url">去百度</a>
        <br/>
        <a :href="https://www.cnblogs.com/codechen8848/archive/2022/10/19/school.url">去百度</a>
    </div>

    <script>
        // 阻止 Vue 在啟動時生成生產提示
        Vue.config.productionTip = false

        new Vue({
            el: "#root",
            data: {
                name: "張三",
                school: {
                    url: "http://baidu.com"
                }
            }
        })
    </script>
</body>
</html>

1.4 資料系結

1.4.1 單向資料系結

  1. 語法:v-bind:href="https://www.cnblogs.com/codechen8848/archive/2022/10/19/xxx" 或簡寫為 :href
  2. 特點:資料只能從 data 流向頁面

1.4.2 雙向資料系結

  1. 語法:v-model:value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/xxx" 或簡寫為 v-model="xxx"
  2. 特點:資料不僅能從 data 流向頁面,還能從頁面流向 data

1.4.3 示例代碼

<!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>資料系結</title>
    <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
</head>
<body>

    <!--  Vue中有2種資料系結的方式:
            1.單向系結(v-bind):資料只能從 data 流向頁面,
            2.雙向系結(v-model):資料不僅能從 data 流向頁面,還可以從頁面流向 data ,
                備注:
                    1.雙向系結一般都應用在表單類元素上(如:input、select等)
                    2.v-model:value 可以簡寫為 v-model,因為 v-model默認收集的就是 value 值,
    -->
    <div id="root">
        <!-- 單向系結 -->

        <!-- 普通寫法 -->
        單向系結普通寫法:<input type="text" v-bind:value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/name"></input>
        <br/>
        <!-- 簡寫 -->
        單向系結簡寫:<input type="text" :value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/name"></input>
        
        <hr></hr>
        <!-- 雙向系結 -->
        <!-- 普通寫法 -->
        雙向系結普通寫法:<input type="text" v-model:value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/name"></input>
        <br/>
        <!-- 簡寫 -->
        雙向系結簡寫:<input type="text" v-model="name"></input>

        <!-- 如下代碼是錯誤的,因為 v-model只能應用在表單類元素(輸入類元素)上 -->
        <!-- <h2 v-model="name">: v-model is not supported on this element type. 
            If you are working with contenteditable, it's recommended to wrap a library dedicated for that purpose inside a custom component. -->
        <h2 v-model:x="name"></h2>
        


    </div>

    <script>
        // 組織 Vue 在啟動時生成生產提示
        Vue.config.productionTip = false

        new Vue({
            el: "#root",
            data: {
                name: "張三"
            }
        })
    </script>
</body>
</html>

1.5 MVVM模型

  1. M:模型(Model):對應 data 中的資料
  2. V:視圖(View):模板
  3. VM:視圖模型(ViewModel):Vue 實體物件

image-20220810144417339

1.6 事件處理

1.6.1 系結監聽

  1. v-on:xxx="fun"
  2. @xxx="fun"
  3. @xxx="fun(引數)"
  4. 默認事件形參:event
  5. 隱含屬性物件:$event
<!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>事件的基本使用</title>
    <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
</head>
<body>
    <!-- 
        事件的基本使用:
            1.使用 v-on:xxx 或 @xxx 系結事件,其中 xxx 是事件名;
            2.事件的回呼需要配置在 vue 實體中的 methods 物件,最侄訓在 vm 實體上;
            3.methods 中配置配置的函式,不要用箭頭函式,否則 this 就不是 vm 實體了;
            4.methods 中配置的函式,都是被 vue 實體所管理的函式,this 的指向是 vm 或 組件實體物件;
            5.@click="demo" 和 @click="demo($event)" 效果一致,但后者可以傳參
     -->
    <div id="root">
        <h2>歡迎來到 {{name}} 學習</h2>
        <button v-on:click = "showInfo01">點我去學習1</button>
        <button v-on:click = "showInfo02">點我去學習2</button>
        <button @click = "showInfo03">點我去學習3</button>
        <button @click = "showInfo04(88)">點我去學習4</button>
        <button @click = "showInfo05(88, $event)">點我去學習5</button>
    </div>
    
    <script>
        // 阻止 vue 在啟動時生成生產提示
        Vue.config.productionTip = false

        new Vue({
            el: "#root",
            data() {
                return {
                    name: "北京大學"
                }
            },
            
            methods: {
                showInfo01() {
                    alert("點我去學習1")
                },
                
                showInfo02(event) {
                    console.log(event);
                },

                showInfo03() {
                    alert("點我去學習3")
                },

                showInfo04(number) {
                    console.log(number);
                    alert("點我去學習4" + number)
                },

                showInfo05(number, event) {
                    console.log(number, event);
                    alert("點我去學習5")
                },
            },
        })
    </script>
</body>
</html>

1.6.2 事件修飾符

  1. prevent: 阻止事件的默認行為 event.preventDefault()
  2. stop: 停止事件冒泡 event.stopPropagation()
<!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>事件修飾符</title>
    <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    <style>
        *{
            margin-top: 20px;
        }
        .box1 {
            height: 50px;
            padding: 5px;
            background-color: skyblue;
        }
        .box2 {
            height: 50px;
            padding: 5px;
            background-color: orange;
        }
        .box3 {
            height: 5px;
            padding: 5px;
            background-color: rebeccapurple;
        }
        .box4 {
            height: 50px;
            padding: 5px;
            background-color: antiquewhite;
        }
        .list {
            width: 200px;
            height: 200px;
            background-color: peru;
            /* 在容器內生成滑動視窗 */
            overflow: auto;
        }
        li {
            height: 100px;
        }
    </style>
</head>
<body>
    <!-- 
        Vue中的事件修飾符:
            1.prevent:阻止默認事件(常用);
            2.stop:阻止事件冒泡(常用);
            3.once:事件只觸發一次(常用);
            4.capture:使用事件的捕獲模式;
            5.self:只有event.target是當前操作的元素時才觸發事件;
            6.passive:事件的默認行為立即執行,無需等待事件回呼執行完畢;
    -->

    <div id="root">
        <!-- 1.阻止默認事件 -->
        <!-- 彈窗點擊確認后會跳轉到百度 -->
        <a href="https://www.baidu.com" @click="gotoBaidu">點我去百度</a><br/>
        <!-- 彈窗點擊確認后不會跳轉到百度 -->
        <a href="https://www.baidu.com" @click.prevent="gotoBaidu">點我去百度</a><br>

        <!-- 2.stop:阻止事件冒泡 -->
        <div class = "box1" @click="showInfo">
            <button @click="showInfo">點我提示資訊</button>
        </div>
        <div class = "box1" @click="showInfo">
            <button @click.stop="showInfo">點我提示資訊</button>
        </div>
        
        <!--  3.once:事件只觸發一次 -->
        <button @click.once="showInfo">點我提示資訊</button>

        <!-- 4.capture:使用事件的捕獲模式 -->
        <div  @click.capture="showMessage(1)">
            div01
            <div  @click.capture="showMessage(2)">
                div02
            </div>
        </div>

        <!-- 5.self:只有event.target是當前操作的元素時才觸發事件 -->
        <div  @click.self="showInfo">
            <button @click="showInfo">點我提示資訊</button>
        </div>

        <!--  6.passive:事件的默認行為立即執行,無需等待事件回呼執行完畢 -->\
        <!--  
            @scroll、@wheel 滾輪滑動事件,兩者的區別:
                1.scroll是頁面滾動條滾動會觸發的事件,而 wheel是滑鼠滾輪滾動會觸發的事件,
                2,一旦滾動條到底部后,滑動滑鼠滾輪繼續滾動,wheel 就會一直觸發,而 scroll 不會觸發
        -->

        <ul  @scroll="list">
        <!-- <ul  @wheel="list"> -->
        <!-- <ul  @wheel.passive="list"> -->
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>

        <!-- 7.多個修飾符 -->
        <div @click="showInfo">
            <a href="https://www.baidu.com" @click="showInfo">點我去百度</a>
            <a href="https://www.baidu.com" @click.prevent.stop="showInfo">點我去百度</a>
        </div>

    </div>

    <script>
        // 阻止 vue 在啟動時生成生產提示
        Vue.config.productionTip = false

        new Vue({
            el: "#root",
            data: {

            },
            methods: {
                gotoBaidu() {
                    alert("點我去百度!")
                },
                showInfo() {
                    alert("hello world")
                },
                showMessage(message) {
                    console.log(message);
                },
                list() {
                    for(let i = 0; i < 10; i++) {
                        console.log("#");
                    }
                    console.log("累壞了");
                }
            },
        })
    </script>
</body>
</html>

1.6.3 按鍵修飾符

  1. keycode: 操作的是某個 keycode 值的鍵
  2. keyName: 操作的某個按鍵名的鍵(少部分)
<!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>鍵盤事件</title>
    <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
</head>
<body>
    <!-- 
        1.Vue中常用的按鍵別名:
            回車 => enter
            洗掉 => delete (捕獲“洗掉”和“退格”鍵)
            退出 => esc
            空格 => space
            換行 => tab (特殊,必須配合keydown去使用)
            上 => up
            下 => down
            左 => left
            右 => right

        2.Vue未提供別名的按鍵,可以使用按鍵原始的key值去系結,但注意要轉為kebab-case(短橫線命名)

        3.系統修飾鍵(用法特殊):ctrl、alt、shift、meta
            (1).配合keyup使用:按下修飾鍵的同時,再按下其他鍵,隨后釋放其他鍵,事件才被觸發,
            (2).配合keydown使用:正常觸發事件,

        4.也可以使用keyCode去指定具體的按鍵(不推薦)

        5.Vue.config.keyCodes.自定義鍵名 = 鍵碼,可以去定制按鍵別名
    -->
    <div id="root">
        <h3>歡迎來到{{name}}學習</h3>
        <!-- @keydown、@keyup兩者的區別:前者是鍵盤按下事件,后者是鍵盤恢復事件 -->
        <input placeholder="回車事件" @keydown="demo" ></input><br/>
        <input placeholder="回車事件" @keydown.enter="demo"></input><br/>
        <input placeholder="回車事件" @keydown.13="demo"></input><br/>
        <input placeholder="回車事件" @keydown.huiche="demo"></input><br/>

        <input placeholder="洗掉事件" @keydown.delete="demo"></input><br/>
        <input placeholder="退出事件" @keydown.esc="demo"></input><br/>
        <input placeholder="空格事件" @keydown.space="demo"></input><br/>
        <input placeholder="換行事件" @keydown.tab="demo"></input><br/>
        <input placeholder="上事件" @keydown.up="demo"></input><br/>
        <input placeholder="下事件" @keydown.down="demo"></input><br/>
        <input placeholder="左事件" @keydown.left="demo"></input><br/>
        <input placeholder="右事件" @keydown.right="demo"></input><br/>
    </div>

    <script>
        // 阻止 vue 在啟動時生成生產提示
        Vue.config.productionTip = false
        // 自定義鍵盤別名
        Vue.config.keyCodes.huiche = 13

        new Vue({
            el: "#root",
            data: {
                name: "Blili"
            },
            methods: {
                demo(event) {
                    // 輸出鍵值
                    console.log(event.key, event.keyCode);
                    console.log("hello world");
                }
            },
        })
    </script>
</body>
</html>

1.7 計算屬性與監視屬性

1.7.1 計算屬性 computed

  1. 要顯示的資料不存在,要通過計算得來,

  2. computed 物件中定義計算屬性,

  3. 在頁面中使用插值語法來顯示計算的結果,

    <!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>姓名案例-計算屬性實作</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <!-- 
            計算屬性:
                1.定義:要用的屬性不存在,要通過已有屬性計算得來,
                2.原理:底層借助了 Objcet.defineproperty 方法提供的 getter 和 setter ,
                3.get 函式什么時候執行?
                    (1).初次讀取時會執行一次,
                    (2).當依賴的資料發生改變時會被再次呼叫,
                4.優勢:與 methods 實作相比,內部有快取機制(復用),效率更高,除錯方便,
                5.備注:
                    1.計算屬性最侄訓出現在 vm 上,直接讀取使用即可,
                    2.如果計算屬性要被修改,那必須寫 set 函式去回應修改,且 set 中要引起計算時依賴的資料發生改變,
         -->
        <div id="root">
            姓:<input type="text" v-model="firstName"> <br/>
            名:<input type="text" v-model="lastName"> <br/>
            <!-- 測驗:<input type="text" v-model="x"> <br/> -->
            全名:<span>{{fullName}}</span> <br/>
            <!-- 全名:<span>{{fullName}}</span> <br/>
            全名:<span>{{fullName}}</span> <br/>
            全名:<span>{{fullName}}</span> -->
    
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            const vm = new Vue({
                el: "#root",
                data: {
                    firstName: "張",
                    lastName: "三",
                },
                computed: {
                    fullName: {
                        get() {
                            //get 有什么作用?當有人讀取 fullName 時,get 就會被呼叫,且回傳值就作為 fullName 的值
    					    //get 什么時候呼叫?1.初次讀取 fullName 時,2.所依賴的資料發生變化時,
                            console.log("get 被呼叫了");
                            return this.firstName + " - " + this.lastName
                        },
                        // set 什么時候被呼叫?fullName 被修改時
                        set(value) {
                            console.log("set 被呼叫了");
                            const arr = value.split("-")
                            this.firstName = arr[0]
                            this.lastName = arr[1]
                        }
                    }
                }
            });
        </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>姓名案例-計算屬性簡寫</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <div id="root">
            姓:<input type="text" v-model="firstName"> <br/>
            名:<input type="text" v-model="lastName"> <br/>
            <!-- 測驗:<input type="text" v-model="x"> <br/> -->
            全名:<span>{{fullName}}</span> <br/>
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            new Vue({
                el: "#root",
                data: {
                    firstName: "張",
                    lastName: "三",
                },
                computed: {
                    // fullName:function() {
                    //     return this.firstName + " - " + this.lastName
                    // },
                    fullName() {
                        return this.firstName + " - " + this.lastName
                    }
                }
            })
        </script>
    </body>
    </html>
    

1.7.2 監視屬性 watch

  1. 通過通過 vm 物件的 $watch()watch 配置來監視指定的屬性

  2. 當屬性變化時,回呼函式自動呼叫,在函式內部進行計算

    <!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>天氣案例-監視屬性</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <!-- 
            監視屬性 watch:
                1.當被監視的屬性變化時, 回呼函式自動呼叫, 進行相關操作
                2.監視的屬性必須存在,才能進行監視!!
                3.監視的兩種寫法:
                    (1).new Vue 時傳入 watch 配置
                    (2).通過 vm.$watch 監視
         -->
        <div id="root">
            <h2>今天天氣很{{info}}</h2>
            <button @click="changeWeather">切換天氣</button>
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            const vm = new Vue({
                el: "#root",
                data: {
                    isHot: true
                },
                computed: {
                    info() {
                        return this.isHot ? "炎熱":"涼爽";
                    }
                },
                methods: {
                    changeWeather() {
                        this.isHot = !this.isHot
                    }
                },
                watch: {
                    // isHot: {
                    //     // 初始化的時候讓 handler 呼叫一下
                    //     immediate: true,
                    //     // handler 什么時候被呼叫?當isHot值發生變化時
                    //     handler(newValue, oldValue) {
                    //         console.log("isHot 值被修改了", newValue, oldValue);
                    //     }
                    // },
                    
                    // computed 中定義的屬性也能被監視
                    info: {
                        handler(newValue, oldValue) {
                            console.log("info值被修改了", newValue, oldValue);
                        }
                    }
                }
            });
    
            // watch 屬性的另外一種寫法
            vm.$watch("isHot", {
                // 初始化的時候讓 handler 呼叫一下
                immediate: true,
                // handler 什么時候被呼叫?當isHot值發生變化時
                handler(newValue, oldValue) {
                    console.log("isHot 值被修改了", newValue, oldValue);
                }
            })
        </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>天氣案例-深度監視</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <!-- 
            深度監視:
                (1).Vue 中的 watch 默認不監測物件內部值的改變(一層),
                (2).配置 deep:true 可以監測物件內部值改變(多層),
                備注:
                    (1).Vue 自身可以監測物件內部值的改變,但 Vue 提供的 watch 默認不可以!
                    (2).使用 watch 時根據資料的具體結構,決定是否采用深度監視,
    
         -->
        <div id="root">
            <h2>今天天氣很{{info}}</h2>
            <button @click="changeWeather">切換天氣</button>
            <hr/>
            <h3>x的值是:{{numbers.x}}</h3>
            <button @click="numbers.x ++">點我讓x + 1</button>
            <h3>y的值是:{{numbers.y}}</h3>
            <button @click="numbers.y ++">點我讓y + 1</button>
            <br/>
            <br/>
            <button @click="numbers = {x:666, y:888}">徹底替換掉numbers</button>
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            new Vue({
                el: "#root",
                data: {
                    isHot: true,
                    numbers: {
                        x: 100,
                        y: 200
                    },
                    a: {
                        b: {
                            c: {
                                d: "e"
                            }
                        }
                    }
                },
                computed: {
                    info() {
                        return this.isHot ? "炎熱":"涼爽";
                    }
                },
                methods: {
                    changeWeather() {
                        this.isHot = !this.isHot
                    }
                },
                watch: {
                    isHot: {
                        // 初始化的時候讓 handler 呼叫一下
                        immediate: true,
                        // handler 什么時候被呼叫?當isHot值發生變化時
                        handler(newValue, oldValue) {
                            console.log("isHot 值被修改了", newValue, oldValue);
                        }
                    },
                    
                    // computed 中定義的屬性也能被監視
                    info: {
                        handler(newValue, oldValue) {
                            console.log("info值被修改了", newValue, oldValue);
                        }
                    },
                    // 監視多級結構中某個屬性的變化
                    "numbers.x": {
                        handler(newValue, oldValue) {
                            console.log("numbers.x 值被修改了", newValue, oldValue);
                        }
                    },
                    //監視多級結構中所有屬性的變化
                    numbers: {
                        deep: true,
                        handler(newValue, oldValue) {
                            console.log("numbers 值被修改了", newValue, oldValue);
                        }
                    }
                }
            })
        </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>天氣案例-監視屬性-簡寫</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <div id="root">
            <h2>今天天氣很{{info}}</h2>
            <button @click="changeWeather">切換天氣</button>
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            const vm = new Vue({
                el: "#root",
                data: {
                    isHot: true
                },
                computed: {
                    info() {
                        return this.isHot ? "炎熱":"涼爽"
                    }
                },
                methods: {
                    changeWeather() {
                        this.isHot = !this.isHot
                    }
                },
                watch: {
                    // 正常寫法
                    // isHot: {
                    //     // 在初始化時呼叫 handler 一次
                    //     immediate: true,
                    //     handler(newValue, oldValue) {
                    //         console.log("isHot 值被修改了", newValue, oldValue);
                    //     }
                    // },
    
                    // 簡寫
                    // 簡寫的缺點時不能寫屬性資訊
                    // isHot(newValue, oldValue) {
                    //     console.log("isHot 值被修改了", newValue, oldValue);
                    // }
                },
            });
    
            // 正常寫法
            // vm.$watch("isHot", {
            //     // 初始化的時候讓 handler 呼叫一下
            //     immediate: true,
            //     // handler 什么時候被呼叫?當isHot值發生變化時
            //     handler(newValue, oldValue) {
            //         console.log("isHot 值被修改了", newValue, oldValue);
            //     }
            // })
            // 簡寫
            vm.$watch("isHot", function(newValue, oldValue) {
                console.log("isHot 值被修改了", newValue, oldValue);
            })
        </script>
    </body>
    </html>
    

1.8 classstyle 系結

1.8.1 理解

  1. 在應用界面中,某個(些)元素的樣式是變化的
  2. class/style 系結就是專門用來實作動態樣式效果的技術

1.8.2 class 系結

  1. :class='xxx'
  2. 運算式是字串:'classA'
  3. 運算式是物件:{classA:isA,classB:isB}
  4. 運算式是陣列:['classA','classB']

1.8.3 style 系結

  1. :style="{color:activeColor,fontSize:fontSize+'px'}"
  2. 其中 activeColor/fontSizedata 屬性

1.8.4 示例代碼

<!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>系結樣式</title>
    <style>
        .basic{
            width: 400px;
            height: 100px;
            border: 1px solid black;
        }
        
        .happy{
            border: 4px solid red;;
            background-color: rgba(255, 255, 0, 0.644);
            background: linear-gradient(30deg,yellow,pink,orange,yellow);
        }
        .sad{
            border: 4px dashed rgb(2, 197, 2);
            background-color: gray;
        }
        .normal{
            background-color: skyblue;
        }

        .box1{
            background-color: yellowgreen;
        }
        .box2{
            font-size: 30px;
            text-shadow:2px 2px 10px red;
        }
        .box3{
            border-radius: 20px;
        }
    </style>
    <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
</head>
<body>
    <!-- 
        系結樣式:
            1. class樣式
                寫法: xxx可以是字串、物件、陣列,
                    字串寫法適用于:類名不確定,要動態獲取,
                    物件寫法適用于:要系結多個樣式,個數不確定,名字也不確定,
                    陣列寫法適用于:要系結多個樣式,個數確定,名字也確定,但不確定用不用,
            2. style樣式
                :style="{fontSize: xxx}"其中xxx是動態值,
                :style="[a,b]"其中a、b是樣式物件,
            3. 二者的區別:
                :class 動態系結 class 的名稱,然后專門在 <style></style> 中去設定對應 class 的樣式
                :style 動態系結 style 的效果,直接把 css 寫在里面

     -->
    <div id="root">

        <!-- 系結 class 樣式(字串寫法),適用于:樣式的類名不確定,需要動態指定 -->
        <div  : @click="changeMood1">{{name}}</div> </br>

        <!-- 系結 class 樣式(陣列寫法),適用于:要系結的樣式個數不確定、名字也不確定-->
        <div  :>{{name}}</div> </br>

        <!-- 系結 class 樣式(物件寫法),適用于:要系結的樣式個數確定、名字也確定,但要動態決定用不用 -->
        <div  :>{{name}}</div> </br>
        
        <!-- 系結 style 樣式(物件寫法) -->
        <div  :style="{fontSize: '40px', color: 'orange'}">{{name}}</div> </br>
        <div  :style="styleObj">{{name}}</div> </br>

        <!-- 系結 style 樣式(陣列寫法) -->
        <div  :style="styleArr">{{name}}</div> </br>


    </div>

    <script>
        // 阻止 vue 在啟動時生成生產提示
        Vue.config.productionTip = false

        new Vue({
            el: "#root",
            data: {
                name: "Hello World",
                mood: "happy",
                moodArr: ["box1", "box2", "box3"],
                moodObj: {
                    box1: true,
                    box2: false,
                    box3: true
                },
                styleObj: {
                    fontSize: "40px",
                    color: "red"
                },
                styleArr: [
                    {
                        fontSize: "40px",
                        color: "blue"
                    },
                    {
                        backgroundColor: "green"
                    }
                ]
            },
            methods: {
                changeMood1() {
                    this.mood = "sad"
                },
            },
        })
    </script>
</body>
</html>

1.9 條件渲染

1.9.1 條件渲染指令

  1. v-ifv-else
  2. v-show

1.9.2 比較 v-ifv-show

  1. 如果需要頻繁切換 v-show 較好
  2. 當條件不成立時,v-if 的所有子節點不會決議(專案中使用)

1.9.3 示例代碼

<!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>條件渲染</title>
    <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
</head>
<body>
    <!-- 
        條件渲染:
            1.v-if
                寫法:
                        (1).v-if="運算式" 
                        (2).v-else-if="運算式"
                        (3).v-else="運算式"
                適用于:切換頻率較低的場景,
                特點:不展示的 DOM 元素直接被移除,
                注意:v-if 可以和: v-else-if、v-else 一起使用,但要求結構不能被“打斷”,

            2.v-show
                寫法:v-show="運算式"
                適用于:切換頻率較高的場景,
                特點:不展示的 DOM 元素未被移除,僅僅是使用樣式隱藏掉
                
            3.備注:使用 v-if 時,元素可能無法獲取到,而使用 v-show 一定可以獲取到,

    -->
    <div id="root">
        <!-- 使用 v-show 來做條件渲染 -->
        <!-- <h2 style="display: none;"> 歡迎來到百度</h2> -->
        <h2 v-show="false"> 歡迎來到{{name}}</h2>
        <h2 v-show=" 1 === 1"> 歡迎來到{{name}}</h2>
        
        <!-- 使用 v-if 來做條件渲染 -->
        <h2 v-if="false"> 歡迎來到{{name}}</h2>
        <h2 v-if="2 === 2"> 歡迎來到{{name}}</h2>

        <!-- v-if與template的配合使用 -->
        <template v-if="n === 1">
            <h2>Angular</h2>
            <h2>React</h2>
            <h2>Vue</h2>
        </template>


    </div>

    <script>
        // 阻止 vue 在啟動時生成生產提示
        Vue.config.productionTip = false

        new Vue({
            el: "#root",
            data: {
                name: "百度",
                n: 1
            }
        })
    </script>
</body>
</html>

1.10 串列渲染

1.10.1 串列顯示指令

  1. 遍歷陣列:v-for/index
  2. 遍歷物件:v-for/key

1.10.2 示例代碼

  1. 基本串列

    <!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>基本串列</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <!-- 
            v-for指令:
                1.用于展示串列資料
                2.語法:v-for="(item, index) in xxx" :key="yyy"
                3.可遍歷:陣列、物件、字串(用的很少)、指定次數(用的很少)
        -->
        <div id="root">
            <!-- 遍歷陣列 -->
            <h2>遍歷陣列</h2>
            <!-- 用 id 作為 key -->
            <!-- <ul v-for="p in personList" :key = "p.id">
                <li>{{p.name}} - {{p.age}}</li>
            </ul> -->
            <!-- 用索引 index 作為 key -->
            <ul v-for="(p, index) in personList" :key = "index">
                <li>{{p.name}} - {{p.age}}</li>
            </ul>
            <hr/>
            
            <!-- 遍歷物件 -->
            <h2>遍歷物件</h2>
            <!-- 注意:key 在后面,value 在前面 -->
            <ul v-for="(v, k) in car" ::key="k">
                <li>{{k}} - {{v}}</li>
            </ul>
            <hr/>
            
            <!-- 遍歷字串 -->
            <h2>遍歷字串</h2>
            <ul v-for="(char, index) in str" :key="index">
                <ul>{{index}} - {{char}}</ul>
            </ul>
            <hr/>
    
            <!-- 遍歷指定次數 -->
            <h2>遍歷指定次數</h2>
            <ul v-for="i in 5">
                <ul>{{i}}</ul>
            </ul>
    
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            new Vue({
                el: "#root",
                data: {
                    personList: [
                        {id: "001", name: "張三", age: 18},
                        {id: "002", name: "李四", age: 19},
                        {id: "003", name: "王五", age: 20},
                    ],
                    car: {
                        name: "奧迪 A8",
                        price: "80萬",
                        color: "黑色"
                    },
                    str: "hello world"
                }
            })
        </script>
    </body>
    </html>
    
  2. key 的原理

    <!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>key的原理</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <!-- 
            面試題:react、vue 中的 key 有什么作用?(key 的內部原理)
                1. 虛擬 DOM 中 key 的作用:
                    key 是虛擬 DOM 物件的標識,當資料發生變化時,Vue 會根據【新資料】生成【新的虛擬 DOM】, 
                    隨后 Vue 進行【新虛擬 DOM】與【舊虛擬 DOM】的差異比較,比較規則如下:
                2.對比規則:
                    (1).舊虛擬 DOM 中找到了與新虛擬 DOM 相同的 key:
                        ①.若虛擬 DOM 中內容沒變, 直接使用之前的真實 DOM!
                        ②.若虛擬 DOM 中內容變了, 則生成新的真實 DOM,隨后替換掉頁面中之前的真實 DOM,
                    (2).舊虛擬 DOM 中未找到與新虛擬 DOM 相同的 key
                        創建新的真實DOM,隨后渲染到到頁面,
                3. 用 index 作為 key 可能會引發的問題:
                    1. 若對資料進行:逆序添加、逆序洗掉等破壞順序操作:
                                    會產生沒有必要的真實 DOM 更新 ==> 界面效果沒問題, 但效率低,
                    2. 如果結構中還包含輸入類的 DOM:
                                    會產生錯誤 DOM 更新 ==> 界面有問題,
                4. 開發中如何選擇 key?:
                    1.最好使用每條資料的唯一標識作為 key, 比如 id、手機號、身份證號、學號等唯一值,
                    2.如果不存在對資料的逆序添加、逆序洗掉等破壞順序操作,僅用于渲染串列用于展示,
                        使用 index 作為 key 是沒有問題的,
        -->
        <div id="root">
            <h2>遍歷陣列</h2>
            <button @click="add">在前面添加一個趙六</button>
            <!-- <ul v-for="(p, index) in personList" :key = "p.id"> -->
            <ul v-for="(p, index) in personList" :key = "index">
                <li>
                    {{p.name}} - {{p.age}}
                    <input type="text"></input>
                </li>
            </ul>
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            new Vue({
                el: "#root",
                data: {
                    personList: [
                        {id: "001", name: "張三", age: 18},
                        {id: "002", name: "李四", age: 19},
                        {id: "003", name: "王五", age: 20},
                    ],
                },
                methods: {
                    add() {
                        this.personList.unshift({
                            id: "004", name: "趙六", age: 38
                        })
                    }
                },
            })
        </script>
    </body>
    </html>
    
  3. 串列過濾

    <!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>串列過濾</title></title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <div id="root">
            <h2>遍歷陣列</h2>
            <input type="text" placeholder="輸入名字進行過濾" v-model="keyWord"></input>
            <ul v-for="(p, index) in filterPersonList" ::key="index">
                <li>{{p.name}} - {{p.age}} - {{p.sex}}</li>
            </ul>
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            new Vue({
                el: "#root",
                data: {
                    keyWord:'',
                    personList:[
                        {id:'001',name:'馬冬梅',age:19,sex:'女'},
                        {id:'002',name:'周冬雨',age:20,sex:'女'},
                        {id:'003',name:'周杰倫',age:21,sex:'男'},
                        {id:'004',name:'溫兆倫',age:22,sex:'男'}
                    ],
                    filterPersonList: []
                },
                // 用 comouted 實作
                computed: {
                    // filterPersonList() {
                    //     return this.personList.filter((item) => {
                    //         return item.name.indexOf(this.keyWord) > -1
                    //     })
                    // }
                },
                // 用 watch 實作
                watch: {
                    keyWord: {
                        immediate: true,
                        handler(val) {
                            this.filterPersonList = this.personList.filter((item) => {
                                return item.name.indexOf(this.keyWord) > -1
                            })
                        }
                    }
                }
            })
        </script>
    </body>
    </html>
    
  4. 串列排序

    <!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>串列排序</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <div id="root">
            <h2>遍歷陣列</h2>
            <input type="text" placeholder="輸入名字進行過濾" v-model="keyWord"></input>
            <button @click="sortType = 1">升序排序</button></button>
            <button @click="sortType = 2">降序排列</button></button>
            <button @click="sortType = 0">原序排序</button></button>
            <ul v-for="(p, index) in filterPersonList" ::key="index">
                <li>{{p.name}} - {{p.age}} - {{p.sex}}</li>
            </ul>
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            new Vue({
                el: "#root",
                data: {
                    keyWord:'',
                    personList:[
                        {id:'001',name:'馬冬梅',age:19,sex:'女'},
                        {id:'002',name:'周冬雨',age:20,sex:'女'},
                        {id:'003',name:'周杰倫',age:21,sex:'男'},
                        {id:'004',name:'溫兆倫',age:22,sex:'男'}
                    ],
                    sortType: 1
                },
                 // 用 comouted 實作
                computed: {
                    filterPersonList() {
                        const arr = this.personList.filter((item) => {
                            return item.name.indexOf(this.keyWord) > -1
                        })
                        if (this.sortType) {
                            arr.sort((p1, p2) => {
                                return this.sortType === 1 ? p1.age - p2.age : p2.age - p1.age
                            })
                        }
                        return arr
                    }
                },
            })
        </script>
    </body>
    </html>
    
  5. 更新時的一個問題

    <!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>更新時的一個問題</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <div id="root">
            <h2>人員串列</h2>
            <button @click="updateMa">更新馬冬梅的資訊</button>
            <ul>
                <li v-for="p in personList" :key="p.id">
                    {{p.name}} - {{p.age}} - {{p.sex}}
                </li>
            </ul>
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            new Vue({
                el: "#root",
                data: {
                    personList: [
                        {id:'001',name:'馬冬梅',age:19,sex:'女'},
                        {id:'002',name:'周冬雨',age:20,sex:'女'},
                        {id:'003',name:'周杰倫',age:21,sex:'男'},
                        {id:'004',name:'溫兆倫',age:22,sex:'男'}
                    ]
                },
                methods: {
                    updateMa() {
                        // 奏效的寫法
                        // this.personList[0].name = "馬老師"
                        // this.personList[0].age = 50
                        // this.personList[0].sex = "男"
    
                        // 不奏效的寫法
                        this.personList[0] = {name: "馬老師", age: 50, sex: "男"}
                    }
                },
            })
        </script>
    </body>
    </html>
    
  6. 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>Vue 資料檢測改變的原理-物件</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <div id="root">
            <h2>學校名稱: {{name}}</h2>
            <h2>學校地址: {{address}}</h2>
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            new Vue({
                el: "#root",
                data: {
                    name: "北京大學",
                    address: "北京",
                    student: {
                        name: "tom",
                        age: {
                            rAge: 40,
                            sAge: 18
                        },
                        friends: [
                            {name: "jack", age: 38}
                        ]
                    }
                }
            })
        </script>
    </body>
    </html>
    
  7. 模擬一個資料監測

    <!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>模擬一個資料檢測</title>
    </head>
    <body>
        <div id="root">
    
        </div>
        <script>
            let data = https://www.cnblogs.com/codechen8848/archive/2022/10/19/{
                name:"北京大學",
                address: "北京",
                student: {
                    name: "tom",
                    age: 18
                }
            }
    
            // 創建一個監視的實體物件,用于監視 data 中屬性的變化
            const obs = new Observer(data);
    
            // 準備一個 vm 實體物件
            let vm = {}
            vm._data = https://www.cnblogs.com/codechen8848/archive/2022/10/19/data = obs
    
            function Observer(obj) {
                // 匯總物件中所有的屬性形成一個陣列
                const keys = Object.keys(obj);
    
                // 遍歷
                keys.forEach((k) => {
                    Object.defineProperty(this, k, {
                        get() {
                            return obj[k]
                        },
                        set(val) {
                            obj[k] = val;
                        }
                    })
                })
            }
            // 存在的問題是對于深層次的屬性無法生成 get set 如:student 中的屬性
        </script>
    
    
    
  8. Vue.set 的使用

    <!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>Vue.set的使用</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <div id="root">
            <h1>學校資訊</h1>
            <h2>學校名稱:{{shcool.name}}</h2>
            <h2>學校地址:{{shcool.address}}</h2>
            <hr/>
            <h1>學生資訊</h1>
            <button @click="addSex">添加性別</button>
            <h2>學生姓名:{{student.name}}</h2>
            <h2>學生年齡:真實-{{student.age.rAge}} 對外-{{student.age.sAge}}</h2>
            <h2 v-if="student.sex">學生性別:{{student.sex}}</h2>
            <h2>學生朋友</h2>
            <ul>
                <li v-for="(f, index) in student.friends" :key="index">
                    {{f.name}} - {{f.age}}
                </li>
            </ul>
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            new Vue({
                el: "#root",
                data: {
                    shcool: {
                        name: "北京大學",
                        address: "北京"
                    },
                    student: {
                        name: "tom",
                        age: {
                            rAge: 40,
                            sAge: 18
                        },
                        friends: [
                            {name: "jerry", age: 35},
                            {name: "tony", age: 36}
                        ]
                    }
                },
                methods: {
                    addSex() {
                        // 寫法一
                        // Vue.set(this.student, 'sex', "男")
                        // 寫法二
                        this.$set(this.student, 'sex', "女")
                    }
                },
            })
        </script>
    </body>
    </html>
    
  9. 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>Vue監測資料改變的原理-陣列</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <div id="root">
            <h1>學校資訊</h1>
            <h2>學校名稱:{{school.name}}</h2>
            <h2>學校地址:{{school.address}}</h2>
            <h2>校長是:{{school.leader}}</h2>
            <hr/>
            <h1>學生資訊</h1>
            <button @click="addHobby">在最前面添加一個愛好</button>
            <button @click="updateHobby">修改第一個愛好</button>
            <h2>姓名:{{student.name}}</h2>
            <h2 v-if="student.sex">性別:{{student.sex}}</h2>
            <h2>年齡:真實{{student.age.rAge}},對外{{student.age.sAge}}</h2>
            <h2>愛好</h2>
            <ul>
                <li v-for="(h,index) in student.hobby" :key="index">
                    {{h}}
                </li>
            </ul>
            <h2>朋友們</h2>
            <ul>
                <li v-for="(f,index) in student.friends" :key="index">
                    {{f.name}}--{{f.age}}
                </li>
            </ul>
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            new Vue({
                el: "#root",
                data: {
                    school:{
    					name: "北京大學",
    					address: "北京",
    				},
    				student:{
    					name: "tom",
    					age:{
    						rAge: 40,
    						sAge: 29,
    					},
    					hobby: ["抽煙", "喝酒", "燙頭"],
    					friends: [
    						{ name: "jerry", age: 35},
    						{ name: "tony", age: 36}
    					]
    				}
                },
                methods: {
                    addHobby() {
                        this.student.hobby.unshift("睡覺")
                    },
                    updateHobby() {
                        // 不起作用
                        // this.student.hobby[0] = "唱跳"
                        
                        // 方式一
                        // Vue.set(this.student.hobby, 0, '唱跳')
    
                        // 方式二
                        // this.$set(this.student.hobby, 0, "唱跳")
    
                        // 方式三
                        this.student.hobby.splice(0, 1, "唱跳")
    
                    }
                },
            })
        </script>
    </body>
    </html>
    
  10. 總結 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>總結Vue中的資料監測</title>
        <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    </head>
    <body>
        <!-- 
            Vue監視資料的原理:
                1. Vue會監視 data 中所有層次的資料,
    
                2. 如何監測物件中的資料?
                    通過 setter 實作監視,且要在 new Vue 時就傳入要監測的資料,
                    (1).物件中后追加的屬性,Vue 默認不做回應式處理
                    (2).如需給后添加的屬性做回應式,請使用如下API:
                        Vue.set(target,propertyName/index,value) 或 
                        vm.$set(target,propertyName/index,value)
    
                3. 如何監測陣列中的資料?
                    通過包裹陣列更新元素的方法實作,本質就是做了兩件事:
                        (1).呼叫原生對應的方法對陣列進行更新,
                        (2).重新決議模板,進而更新頁面,
    
                4.在 Vue 修改陣列中的某個元素一定要用如下方法:
                    1.使用這些 API: push()、pop()、shift()、unshift()、splice()、sort()、reverse()
                    2.Vue.set() 或 vm.$set()
                特別注意:Vue.set() 和 vm.$set() 不能給 vm 或 vm的根資料物件 添加屬性!!!
        -->
        <div id="root">
            <h1>學生資訊</h1>
            <button @click="addSex">添加性別</button> <br/>
            <button @click="updateSex">修改性別</button> <br/>
            <button @click="addFriend">在串列首位添加一個朋友</button> <br/>
            <button @click="updateFirstFriendName">修改第一個朋友的名字為:趙六</button> <br/>
            <button @click="addHobby">添加一個愛好</button> <br/>
            <button @click="updateHobby">修改第一個愛好為:籃球</button> <br/>
            <button @click="removeRap">過濾掉愛好中的rap</button> <br/>
            <h2>姓名: {{ student.name }}</h2>
            <h2>年齡: {{ student.age }}</h2>
            <h2 v-if="student.sex">性別: {{ student.sex }}</h2>
            <h2>愛好: </h2>
            <ul>
                <li v-for="(h, index) in student.hobby" :key="index">
                    {{ h }}
                </li>
            </ul>
            <h2>朋友: </h2>
            <ul>
                <li v-for="(f, index) in student.friends" :key="index">
                    {{ f.name }} - {{f.age}}
                </li>
            </ul>
        </div>
    
        <script>
            // 阻止 vue 在啟動時生成生產提示
            Vue.config.productionTip = false
    
            new Vue({
                el: "#root",
                data: {
                    student: {
                        name: "張三",
                        age: 18,
                        hobby: ["唱", "跳", "rap"],
                        friends: [
                            { name: "李四", age: 20},
                            { name: "王五", age: 19},
                        ]
                    }
                },
                methods: {
                    addSex() {
                        // 不起作用
                        // this.student.sex = "男"
    
                        // 方式一
                        // Vue.set(this.student, "sex", "男")
    
                        // 方式二
                        this.$set(this.student, "sex", "男")
                    },
                    updateSex() {
                        this.student.sex = "女"
                    },
                    addFriend() {
                        this.student.friends.unshift({ name: "老王", age: 28})
                    },
                    updateFirstFriendName() {
                        this.student.friends[0].name = "趙六"
                    },
                    addHobby() {
                        this.student.hobby.push("籃球")
                    },
                    updateHobby() {
                        // 不起作用
                        // this.student.hobby[0] = "籃球"
    
                        // 方式一
                        // Vue.set(this.student.hobby, 0, "籃球")
    
                        // 方式二
                        // this.$set(this.student.hobby, 0, "籃球")
    
                        // 方式三
                        this.student.hobby.splice(0, 1, "籃球")
                    },
                    removeRap() {
                        this.student.hobby = this.student.hobby.filter(item => {
                            return item !== "rap"
                        })
                    }
                },
            })
        </script>
    </body>
    </html>
    

1.11 收集表單資料

<!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>收集表單資料</title>
    <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
</head>
<body>
    <!-- 
        收集表單資料:
            若:<input type="text"/>,則 v-model收集的是 value 值,用戶輸入的就是 value 值,
            若:<input type="radio"/>,則 v-model收集的是value值,且要給標簽配置 value 值,
            若:<input type="checkbox"/>
                1.沒有配置 input 的 value 屬性,那么收集的就是 checked(勾選 or 未勾選,是布林值)
                2.配置 input 的 value 屬性:
                    (1)v-model 的初始值是非陣列,那么收集的就是 checked(勾選 or 未勾選,是布林值)
                    (2)v-model 的初始值是陣列,那么收集的的就是 value 組成的陣列
            備注:v-model的三個修飾符:
                lazy:失去焦點再收集資料
                number:輸入字串轉為有效的數字
                trim:輸入首尾空格過濾
    -->
    <div id="root">
        <form @submit.prevent="demo">
            <!-- 賬號這么寫的好處是:點擊賬號時,滑鼠游標會自動定位到輸入框內 -->
            <label for="account">賬號:</label><input type="text" id="account" v-model.trim="userInfo.account"></input></br>
            年齡:<input type="number" v-model.number="userInfo.age"> <br/><br/>
            密碼:<input type="password" v-model="userInfo.password"></input></br>
            性別:
            <input type="radio" name="sex" v-model="userInfo.sex" value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/male">男</input>
            <input type="radio" name="sex" v-model="userInfo.sex" value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/female">女</input></br>
            愛好:
            <input type="checkbox"  v-model="userInfo.hobby" value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/chang">唱</input>
            <input type="checkbox"  v-model="userInfo.hobby" value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/tiao">跳</input>
            <input type="checkbox"  v-model="userInfo.hobby" value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/rap">Rap</input></br>
            所屬校區
            <select v-model="userInfo.city">
                <option value="">請選擇校區</option>
                <option value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/beijing">北京</option>
                <option value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/shanghai">上海</option>
                <option value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/shenzhen">深圳</option>
                <option value="https://www.cnblogs.com/codechen8848/archive/2022/10/19/wuhan">武漢</option>
            </select>
            <br/><br/>
            其他資訊:
            <textarea v-model.lazy="userInfo.other"></textarea> <br/><br/>
            <input type="checkbox" v-model="userInfo.agree">閱讀并接受<a href="http://www.baidu.com">《用戶協議》</a></br>
            <button>提交</button>
        </form>
    </div>

    <script>
        // 阻止 vue 在啟動時生成生產提示
        Vue.config.productionTip = false

        new Vue({
            el: "#root",
            data: {
                userInfo:{
					account: '',
					age: 18,
					password: '',
					sex: '',
					hobby: [],
					city: '',
					other: '',
					agree: ''
				}
            },
            methods: {
				demo(){
					console.log(JSON.stringify(this.userInfo))
				}
			}

        })
    </script>
</body>
</html>

1.12 過濾器

1.12.1 理解過濾器

  1. 功能:對要顯示的資料進行特定格式化后再顯示
  2. 注意:并沒有改變原本的資料,是產生新的對應的資料

1.12.2 示例代碼

<!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>過濾器</title>
    <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
    <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/dayjs.min.js"></script>
</head>
<body>
    <!-- 
        過濾器:
            定義:對要顯示的資料進行特定格式化后再顯示(適用于一些簡單邏輯的處理),
            語法:
                1.注冊過濾器:Vue.filter(name,callback) 或 new Vue{ filters: {} }
                2.使用過濾器:{{ xxx | 過濾器名}}  或  v-bind:屬性 = "xxx | 過濾器名"
            備注:
                1.過濾器也可以接收額外引數、多個過濾器也可以串聯
                2.并沒有改變原本的資料, 是產生新的對應的資料
    -->
    <div id="root">
        <h2>顯示格式化后的時間</h2>
        <!-- 計算屬性實作 -->
        <h3>計算屬性實作,現在是{{ fmtTime }}</h3>
        <!-- methods 方法實作 -->
        <h3>計算屬性實作,現在是{{ getFmtTime() }}</h3>
        <!-- 過濾器實作 -->
        <h3>過濾器實作,現在是{{ fmtTime | timeFormat }}</h3>
        <!-- 過濾器實作(傳參) -->
        <h3>過濾器實作,現在是{{ fmtTime | timeFormat2("YYYY-MM-DD") | strSlice }}</h3>

    </div>

    <div id="root2">
        <h2>顯示格式化后的時間</h2>
        <!-- 過濾器實作(傳參) -->
        <h3>過濾器實作,現在是{{ msg | strSlice }}</h3>
    </div>

    <script>
        // 阻止 vue 在啟動時生成生產提示
        Vue.config.productionTip = false
        // 定義全域過濾器
        Vue.filter('strSlice', function(value) {
            return value.slice(0, 4)
        })

        new Vue({
            el: "#root",
            data: {
                time: Date.now()
            },
            computed: {
                fmtTime() {
                    return dayjs(this.time).format('YYYY-MM-DD HH:mm:ss')
                }
            },
            methods: {
                getFmtTime() {
                    return dayjs(this.time).format('YYYY年MM月DD日 HH:mm:ss')
                }
            },
            // 區域過濾器
            filters: {
                timeFormat() {
                    return dayjs(this.time).format('YYYY_MM_DD HH:mm:ss')
                },
                timeFormat2(val) {
                    return dayjs(this.time).format(val)
                },
                // strSlice(value) {
                //     return value.slice(0, 4)
                // }
            }
        })

        new Vue({
            el: "#root2",
            data: {
                msg: "hello world"
            }
        })
    </script>
</body>
</html>

1.13 內置指令與自定義指令

1.13.1 常用內置指令

  1. v-text: 更新元素的 textContent
  2. v-html: 更新元素的 innerHTML
  3. v-if: 如果為 true, 當前標簽才會輸出到頁面
  4. v-else: 如果為 false, 當前標簽才會輸出到頁面
  5. v-show: 通過控制 display 樣式來控制顯示/隱藏
  6. v-for: 遍歷陣列/物件
  7. v-on: 系結事件監聽,一般簡寫為 @
  8. v-bind: 系結決議運算式,可以省略 v-bind
  9. v-model: 雙向資料系結
  10. v-cloak: 防止閃現,與 css 配合: [v-cloak]{display:none}

1.13.2 自定義指令

  1. 注冊全域指令

    Vue.directive('my-directive',function(el,binding){
    	el.innerHTML=binding.value.toupperCase()
    })
    
  2. 注冊區域指令

    directives:{
        'my-directive':{
            bind(el,binding){
            	el.innerHTML = binding.value.toupperCase()
            }
        }
    }
    
  3. 使用指令

    v-my-directive='xxx'
    

1.14 Vue實體生命周期

1.14.1 生命周期流程圖

lifecycle.png

1.14.2 vue生命周期分析

  1. 初始化顯示

    • beforeCreate()
    • created()
    • beforeMount()
    • mounted()
  2. 更新狀態: this.xxx=value

    • beforeUpdate()
    • updated()
  3. 銷毀 Vue 實體: vm.$destory()

    • beforeDestory()
    • destoryed()

1.14.3 示例代碼

<!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>分析生命周期</title>
    <script src="https://www.cnblogs.com/codechen8848/archive/2022/10/js/vue.js"></script>
</head>
<body>
    <div id="root">
        <h2 v-text="n"></h2>
        <h2>n 的值: {{ n }}</h2>
        <button @click="addN">點我 n + 1</button> <br/>
        <button @click="destroy">點我銷毀 Vm</button>
    </div>

    <script>
        // 阻止 vue 在啟動時生成生產提示
        Vue.config.productionTip = false

        const vm = new Vue({
            el: "#root",
            data: {
                n: 1
            },
            methods: {
                addN() {
                    this.n++ 
                },
                destroy() {
                    console.log("bye...bye... ");
                    // 銷毀 vm 的方式
                    this.$destroy()
                }
            },
            watch: {
                n() {
                    console.log("n 的值發生改變");
                }
            },
            // 資料監測、資料代理之前,此時無法通過 vm 訪問到 data 中的資料、methods 中的方法
            beforeCreate() {
                console.log("beforeCreate... ")
                console.log("beforeCreate... ", this, this.n) // this.n undefined
                // debugger;
            },
            // 資料監測、資料代理之后,此時可以通過 vm 訪問到 data 中的資料、methods 中的方法
            created() {
                console.log("created... ");
                console.log("created... ", this, this.n);
                // debugger;
            },
            // 此時頁面呈現的是未經 Vue 編譯的 DOM 結構,所有對 DOM 的操作,最終都不奏效
            beforeMount() {
                console.log("beforeMount... ");
                // debugger; // 模板尚未決議
            },
            // 此時頁面中呈現的經 Vue 編譯的 DOM,對 DOM 的操作均有效(盡可能避免)
            // 至此初始化程序結束
            // 一般在此進行:開啟定時器、發送網路請求、訂閱訊息、系結自定義事件等初始化作業
            mounted() {
                console.log("mounted... ")
                // debugger;
            },
            // 此時資料是新的,但頁面是舊的,即頁面尚未和資料保持同步
            beforeUpdate() {
                console.log("beforeUpdate... ")
                // debugger;
            },
            // 此時資料是新的,但頁面也是新的,即頁面和資料保持同步
            updated() {
                console.log("updated... ")
                // debugger;
            },
            // 此時 Vm 中的所有 data、methods、指令等等,都處于可用狀態,馬上要執行銷毀程序
            // 一般在此進行:關閉定時器、取消訂閱訊息、解綁自定義事件等
            beforeDestroy() {
                console.log("beforeDestroy... ")
                // debugger;
            },
            // 銷毀 vm 實體
            destroyed() {
                console.log("destroyed... ")
            },
        })
    </script>
</body>
</html>

1.14.4 常用的生命周期方法

  1. mounted(): 發送 ajax 請求,啟動定時器等異步任務
  2. beforeDestory(): 做收尾作業,如:清除定時器

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/518572.html

標籤:其他

上一篇:一鍵在Web端把CAD圖自動分割成多張圖紙并匯出子圖或圖片

下一篇:超強的蘋果官網滾動文字特效實作

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • IEEE1588PTP在數字化變電站時鐘同步方面的應用

    IEEE1588ptp在數字化變電站時鐘同步方面的應用 京準電子科技官微——ahjzsz 一、電力系統時間同步基本概況 隨著對IEC 61850標準研究的不斷深入,國內外學者提出基于IEC61850通信標準體系建設數字化變電站的發展思路。數字化變電站與常規變電站的顯著區別在于程序層傳統的電流/電壓互 ......

    uj5u.com 2020-09-10 03:51:52 more
  • HTTP request smuggling CL.TE

    CL.TE 簡介 前端通過Content-Length處理請求,通過反向代理或者負載均衡將請求轉發到后端,后端Transfer-Encoding優先級較高,以TE處理請求造成安全問題。 檢測 發送如下資料包 POST / HTTP/1.1 Host: ac391f7e1e9af821806e890 ......

    uj5u.com 2020-09-10 03:52:11 more
  • 網路滲透資料大全單——漏洞庫篇

    網路滲透資料大全單——漏洞庫篇漏洞庫 NVD ——美國國家漏洞庫 →http://nvd.nist.gov/。 CERT ——美國國家應急回應中心 →https://www.us-cert.gov/ OSVDB ——開源漏洞庫 →http://osvdb.org Bugtraq ——賽門鐵克 →ht ......

    uj5u.com 2020-09-10 03:52:15 more
  • 京準講述NTP時鐘服務器應用及原理

    京準講述NTP時鐘服務器應用及原理京準講述NTP時鐘服務器應用及原理 安徽京準電子科技官微——ahjzsz 北斗授時原理 授時是指接識訓通過某種方式獲得本地時間與北斗標準時間的鐘差,然后調整本地時鐘使時差控制在一定的精度范圍內。 衛星導航系統通常由三部分組成:導航授時衛星、地面檢測校正維護系統和用戶 ......

    uj5u.com 2020-09-10 03:52:25 more
  • 利用北斗衛星系統設計NTP網路時間服務器

    利用北斗衛星系統設計NTP網路時間服務器 利用北斗衛星系統設計NTP網路時間服務器 安徽京準電子科技官微——ahjzsz 概述 NTP網路時間服務器是一款支持NTP和SNTP網路時間同步協議,高精度、大容量、高品質的高科技時鐘產品。 NTP網路時間服務器設備采用冗余架構設計,高精度時鐘直接來源于北斗 ......

    uj5u.com 2020-09-10 03:52:35 more
  • 詳細解讀電力系統各種對時方式

    詳細解讀電力系統各種對時方式 詳細解讀電力系統各種對時方式 安徽京準電子科技官微——ahjzsz,更多資料請添加VX 衛星同步時鐘是我京準公司開發研制的應用衛星授時時技術的標準時間顯示和發送的裝置,該裝置以M國全球定位系統(GLOBAL POSITIONING SYSTEM,縮寫為GPS)或者我國北 ......

    uj5u.com 2020-09-10 03:52:45 more
  • 如何保證外包團隊接入企業內網安全

    不管企業規模的大小,只要企業想省錢,那么企業的某些服務就一定會采用外包的形式,然而看似美好又經濟的策略,其實也有不好的一面。下面我通過安全的角度來聊聊使用外包團的安全隱患問題。 先看看什么服務會使用外包的,最常見的就是話務/客服這種需要大量重復性、無技術性的服務,或者是一些銷售外包、特殊的職能外包等 ......

    uj5u.com 2020-09-10 03:52:57 more
  • PHP漏洞之【整型數字型SQL注入】

    0x01 什么是SQL注入 SQL是一種注入攻擊,通過前端帶入后端資料庫進行惡意的SQL陳述句查詢。 0x02 SQL整型注入原理 SQL注入一般發生在動態網站URL地址里,當然也會發生在其它地發,如登錄框等等也會存在注入,只要是和資料庫打交道的地方都有可能存在。 如這里http://192.168. ......

    uj5u.com 2020-09-10 03:55:40 more
  • [GXYCTF2019]禁止套娃

    git泄露獲取原始碼 使用GET傳參,引數為exp 經過三層過濾執行 第一層過濾偽協議,第二層過濾帶引數的函式,第三層過濾一些函式 preg_replace('/[a-z,_]+\((?R)?\)/', NULL, $_GET['exp'] (?R)參考當前正則運算式,相當于匹配函式里的引數 因此傳遞 ......

    uj5u.com 2020-09-10 03:56:07 more
  • 等保2.0實施流程

    流程 結論 ......

    uj5u.com 2020-09-10 03:56:16 more
最新发布
  • 使用Django Rest framework搭建Blog

    在前面的Blog例子中我們使用的是GraphQL, 雖然GraphQL的使用處于上升趨勢,但是Rest API還是使用的更廣泛一些. 所以還是決定回到傳統的rest api framework上來, Django rest framework的官網上給了一個很好用的QuickStart, 我參考Qu ......

    uj5u.com 2023-04-20 08:17:54 more
  • 記錄-new Date() 我忍你很久了!

    這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 大家平時在開發的時候有沒被new Date()折磨過?就是它的諸多怪異的設定讓你每每用的時候,都可能不小心踩坑。造成程式意外出錯,卻一下子找不到問題出處,那叫一個煩透了…… 下面,我就列舉它的“四宗罪”及應用思考 可惡的四宗罪 1. Sa ......

    uj5u.com 2023-04-20 08:17:47 more
  • 使用Vue.js實作文字跑馬燈效果

    實作文字跑馬燈效果,首先用到 substring()截取 和 setInterval計時器 clearInterval()清除計時器 效果如下: 實作代碼如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta ......

    uj5u.com 2023-04-20 08:12:31 more
  • JavaScript 運算子

    JavaScript 運算子/運算子 在 JavaScript 中,有一些運算子可以使代碼更簡潔、易讀和高效。以下是一些常見的運算子: 1、可選鏈運算子(optional chaining operator) ?.是可選鏈運算子(optional chaining operator)。?. 可選鏈操 ......

    uj5u.com 2023-04-20 08:02:25 more
  • CSS—相對單位rem

    一、概述 rem是一個相對長度單位,它的單位長度取決于根標簽html的字體尺寸。rem即root em的意思,中文翻譯為根em。瀏覽器的文本尺寸一般默認為16px,即默認情況下: 1rem = 16px rem布局原理:根據CSS媒體查詢功能,更改根標簽的字體尺寸,實作rem單位隨螢屏尺寸的變化,如 ......

    uj5u.com 2023-04-20 08:02:21 more
  • 我的第一個NPM包:panghu-planebattle-esm(胖虎飛機大戰)使用說明

    好家伙,我的包終于開發完啦 歡迎使用胖虎的飛機大戰包!! 為你的主頁添加色彩 這是一個有趣的網頁小游戲包,使用canvas和js開發 使用ES6模塊化開發 效果圖如下: (覺得圖片太sb的可以自己改) 代碼已開源!! Git: https://gitee.com/tang-and-han-dynas ......

    uj5u.com 2023-04-20 08:01:50 more
  • 如何在 vue3 中使用 jsx/tsx?

    我們都知道,通常情況下我們使用 vue 大多都是用的 SFC(Signle File Component)單檔案組件模式,即一個組件就是一個檔案,但其實 Vue 也是支持使用 JSX 來撰寫組件的。這里不討論 SFC 和 JSX 的好壞,這個仁者見仁智者見智。本篇文章旨在帶領大家快速了解和使用 Vu ......

    uj5u.com 2023-04-20 08:01:37 more
  • 【Vue2.x原始碼系列06】計算屬性computed原理

    本章目標:計算屬性是如何實作的?計算屬性快取原理以及洋蔥模型的應用?在初始化Vue實體時,我們會給每個計算屬性都創建一個對應watcher,我們稱之為計算屬性watcher ......

    uj5u.com 2023-04-20 08:01:31 more
  • http1.1與http2.0

    一、http是什么 通俗來講,http就是計算機通過網路進行通信的規則,是一個基于請求與回應,無狀態的,應用層協議。常用于TCP/IP協議傳輸資料。目前任何終端之間任何一種通信方式都必須按Http協議進行,否則無法連接。tcp(三次握手,四次揮手)。 請求與回應:客戶端請求、服務端回應資料。 無狀態 ......

    uj5u.com 2023-04-20 08:01:10 more
  • http1.1與http2.0

    一、http是什么 通俗來講,http就是計算機通過網路進行通信的規則,是一個基于請求與回應,無狀態的,應用層協議。常用于TCP/IP協議傳輸資料。目前任何終端之間任何一種通信方式都必須按Http協議進行,否則無法連接。tcp(三次握手,四次揮手)。 請求與回應:客戶端請求、服務端回應資料。 無狀態 ......

    uj5u.com 2023-04-20 08:00:32 more