課堂講義
1、Vue 高級使用
1.1、自定義組件
-
學(xué)完了 Element 組件后,我們會發(fā)現(xiàn)組件其實就是自定義的標(biāo)簽。例如<el-button>就是對<button>的封裝
-
本質(zhì)上,組件是帶有一個名字且可復(fù)用的 Vue 實例,我們完全可以自己定義
-
定義格式
Vue.component(組件名稱, { props:組件的屬性, data: 組件的數(shù)據(jù)函數(shù), template: 組件解析的標(biāo)簽?zāi)0?br>})
-
代碼實現(xiàn)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>自定義組件</title> <script src="vue/vue.js"></script> </head> <body> <div id="div"> <my-button></my-button> </div> </body> <script> Vue.component("my-button",{
1.2、Vue的生命周期

-
生命周期的八個階段

-
代碼實現(xiàn)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>生命周期</title> <script src="vue/vue.js"></script> </head> <body> <div id="app"> {{message}} </div> </body> <script> let vm = new Vue({ el: '#app', data: { message: 'Vue的生命周期' }, beforeCreate: function() { console.group('------beforeCreate創(chuàng)建前狀態(tài)------'); console.log("%c%s", "color:red", "el : " + this.$el); |