计算机科学 编程语言 Vue系列-Day01-第一个Vue程序 小码同学 2022-07-31 2025-01-18 第一个VUE程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <!DOCTYPE html > <html > <head > <meta charset ="utf-8" /> <title > </title > <script src ="js/v2.6.10/vue.min.js" type ="text/javascript" charset ="utf-8" > </script > </head > <body > <div id ="app" > {{ counter }} </div > <script > var vm = new Vue ({ el :"#app" , data :{ counter :"www.hikki.site" } }); </script > </body > </html >
if-else
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <!DOCTYPE html > <html > <head > <meta charset ="utf-8" /> <title > </title > <script src ="js/v2.6.10/vue.min.js" type ="text/javascript" charset ="utf-8" > </script > </head > <body > <div id ="app" > <h1 v-if = "test" > www.hikki.site</h1 > <h1 v-else > 这是我的博客</h1 > </div > <script > var vm = new Vue ({ el :"#app" , data :{ test : true } }); </script > </body > </html >
if-else-if
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <!DOCTYPE html > <html > <head > <meta charset ="utf-8" /> <title > </title > <script src ="js/v2.6.10/vue.min.js" type ="text/javascript" charset ="utf-8" > </script > </head > <body > <div id ="app" > <h1 v-if = "test === 'A'" > www.hikki.site</h1 > <h1 v-else-if = "test ==='B'" > B</h1 > <h1 v-else > C</h1 > </div > <script > var vm = new Vue ({ el :"#app" , data :{ test : 'A' } }); </script > </body > </html >
v-for
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <!DOCTYPE html > <html > <head > <meta charset ="utf-8" /> <title > </title > <script src ="js/v2.6.10/vue.min.js" type ="text/javascript" charset ="utf-8" > </script > </head > <body > <div id ="app" > <li v-for ="item in items" > {{item.msg}} </li > </div > <script > var vm = new Vue ({ el :"#app" , data :{ items :[ {msg :'www.hikki.site' }, {msg :'筆記' }, {msg :'我的博客' }, ] } }); </script > </body > </html >
监听变量变化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <!DOCTYPE html > <html > <head > <meta charset ="utf-8" > <title > </title > <script src ="vue.min.js" type ="text/javascript" charset ="utf-8" > </script > </head > <body > <div id ="app" > {{a}} </div > <script type ="text/javascript" > var data = { a : "变量变化前" };var vm = new Vue ({ el : "#app" , data : data }); vm.$watch('a' , function (newVal, oldVal ){ console .log (newVal, oldVal); }) vm.$data .a = "变量变化后" </script > </body > </html >
生命周期的监听
可以指定事件在某个事件完成前后执行,处理特定的的事情。
beforeCreate
created
beforeMount
mounted
beforeUpdate
updated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 <!DOCTYPE html > <html > <head > <meta charset ="utf-8" > <title > </title > <script src ="vue.min.js" type ="text/javascript" charset ="utf-8" > </script > </head > <body > <div id ="app" > {{msg}} </div > <script type ="text/javascript" > var vm = new Vue ({ el : "#app" , data : { msg : "hi vue" , }, beforeCreate :function ( ){ console .log ('beforeCreate' ); }, created :function ( ){ console .log ('created' ); }, beforeMount : function ( ){ console .log ('beforeMount' ); }, mounted : function ( ){ console .log ('mounted' ); }, beforeUpdate : function ( ){ console .log ('beforeUpdate' ); }, updated : function ( ){ console .log ('updated' ); } }); setTimeout (function ( ){ vm.msg = "3秒后改变" ; }, 3000 ); </script > </body > </html >