Vue生命周期中的組件化你知道嗎
引出生命周期
此時調(diào)用change,定時器回調(diào)修改opacity,數(shù)據(jù)修改,模板重新解析,再次調(diào)用change。
銷毀流程
解綁(自定義)事件監(jiān)聽器
生命周期
生命周期總結(jié)
<div id="root"> <!-- <h2 :style="{opacity}">hello,{{name}}</h2> --> <h2 :style="{opacity:opacity}">hello,{{name}}</h2> <button @click="stop">click stop</button> <button @click="opacity = 1">opacity 1</button> </div> <script type="text/javascript"> Vue.config.productionTip = false; new Vue({ el: "#root", data: { name: "atguigu", opacity: 1, }, methods: { stop(){ this.$destroy(); } }, beforeDestroy() { clearInterval(this.timer); }, //vue完成模板解析,并把初始的真實(shí)的dom元素放入頁面后(掛載完畢),會調(diào)用該函數(shù)。 mounted() { this.timer = setInterval(() => { this.opacity -= 0.01; if (this.opacity <= 0) { this.opacity = 1 } }, 16); }, }); </script>
組件化
template:
整個root容器當(dāng)作模板
會直接替換掉root,把template當(dāng)作模板進(jìn)行解析。
非單文件組件
data需要用函數(shù)式寫法
<div id="root"> <h2>{{msg}}</h2> <!--組件標(biāo)簽--> <school> </school> <hr> <student> </student> <student> </student> <hello> </hello> </div> <div id="root2"> </div> <script type="text/javascript"> Vue.config.productionTip = false; //創(chuàng)建school組件 const school = Vue.extend({ template:` <div> <h2>schoolname:{{schoolname}}</h2> <h2>schoolage{{schoolage}}</h2> <button @click='show'>點(diǎn)擊提示</button> </div> `, data(){ return{ schoolname: "atguigu", schoolage:20, } }, methods: { show(){ alert(this.schoolname); } }, }); //創(chuàng)建stu組件 const student = Vue.extend({ template:` <div> <h2>stuname:{{stuname}}</h2> <h2>stuage{{stuage}}</h2> </div> `, data(){ return{ stuname:'tom', stuage:18, } }, }); //創(chuàng)建hello組件 const hello = Vue.extend({ template:` <div> <h2>stuname:{{stuname}}</h2> <h2>stuage{{stuage}}</h2> </div> `, data(){ return{ stuname:'tom', stuage:18, } }, }); //全局注冊組件 Vue.component('hello',hello); new Vue({ el: "#root", data:{ msg:'this is msg' }, //局部注冊組件 components:{ school:school, student, } }); </script>
組件的幾個注意點(diǎn)
組件的嵌套
<!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"> <script type="text/javascript" src="../js/vue.js"></script> <title>Document</title> </head> <body> <div id="root"> </div> <script type="text/javascript"> Vue.config.productionTip = false; //創(chuàng)建student組件 const student = Vue.extend({ template:` <div> <h2>stuname:{{stuname}}</h2> <h2>stuage{{stuage}}</h2> </div> `, data(){ return{ stuname:'tom', stuage:18, } }, }); //創(chuàng)建school組件 const school = Vue.extend({ template:` <div> <h2>schoolname:{{schoolname}}</h2> <h2>schoolage{{schoolage}}</h2> <button @click='show'>點(diǎn)擊提示</button> <student></student> </div> `, data(){ return{ schoolname: "atguigu", schoolage:20, } }, methods: { show(){ alert(this.schoolname); } }, components:{ student:student, } }); //創(chuàng)建hello組件 const hello = Vue.extend({ template:` <div> <h2>{{msg}}</h2> </div> `, data(){ return{ msg:'hello!' } }, }); const app = Vue.extend({ template:` <div> <hello></hello> <school></school> </div> `, components:{ school, hello, } }) //vue new Vue({ template:'<app></app>', el: "#root", //局部注冊組件 components:{ app, } }); </script> </body> </html>
VueComponent
每次調(diào)用extend,都返回了一個VueComponent
<!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"> <script type="text/javascript" src="../js/vue.js"></script> <title>Document</title> </head> <body> <div id="root"> <!--組件標(biāo)簽--> <school> </school> <hello> </hello> </div> <div id="root2"> </div> <script type="text/javascript"> Vue.config.productionTip = false; //創(chuàng)建school組件 const school = Vue.extend({ template: ` <div> <h2>schoolname:{{schoolname}}</h2> <h2>schoolage{{schoolage}}</h2> <button @click='show'>點(diǎn)擊提示</button> </div> `, data() { return { schoolname: "atguigu", schoolage: 20, } }, methods: { show() { console.log(this)//VueComponent實(shí)例對象 vc alert(this.schoolname); } }, }); //創(chuàng)建hello組件 const hello = Vue.extend({ template: ` <div> <h2>hello:{{hello}}</h2> </div> `, data() { return { hello: "hello", } }, }); console.log(school);//一個構(gòu)造函數(shù) console.log(hello);//一個構(gòu)造函數(shù) console.log(school === hello);//false new Vue({ el: "#root", data: { }, //局部注冊組件 components: { school: school, hello:hello, } }); </script> </body> </html>
Vue實(shí)例與組件實(shí)例
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue-router實(shí)現(xiàn)webApp切換頁面動畫效果代碼
本篇文章主要介紹了vue實(shí)現(xiàn)app頁面切換效果實(shí)例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05vue中項(xiàng)目頁面空白但不報(bào)錯產(chǎn)生的原因及分析
這篇文章主要介紹了vue中項(xiàng)目頁面空白但不報(bào)錯產(chǎn)生的原因及分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05當(dāng)啟動vue項(xiàng)目安裝依賴時報(bào)錯的解決方案
這篇文章主要介紹了當(dāng)啟動vue項(xiàng)目安裝依賴時報(bào)錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04使用vuetify實(shí)現(xiàn)全局v-alert消息通知的方法
使用強(qiáng)大的Vuetify開發(fā)前端頁面,結(jié)果發(fā)現(xiàn)官方?jīng)]有提供簡便的全局消息通知組件,所以自己動手寫了一個簡單的組件,接下來通過本文給大家介紹使用vuetify實(shí)現(xiàn)全局v-alert消息通知的詳細(xì)代碼,感興趣的朋友跟隨小編一起看看吧2024-02-02vue里input根據(jù)value改變背景色的實(shí)例
今天小編就為大家分享一篇vue里input根據(jù)value改變背景色的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09vue中的rules表單校驗(yàn)規(guī)則使用方法示例詳解 :rules=“rules“
這篇文章主要介紹了vue中的rules表單校驗(yàn)規(guī)則使用方法示例詳解 :rules=“rules“,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-11-11vue使用exif獲取圖片旋轉(zhuǎn),壓縮的示例代碼
這篇文章主要介紹了vue使用exif獲取圖片旋轉(zhuǎn),壓縮的示例代碼,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下2020-12-12