Vue生命周期中的組件化你知道嗎
更新時間:2022年03月08日 15:53:42 作者:BHW1293773843
這篇文章主要為大家詳細介紹了Vue生命周期中的組件化,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
引出生命周期
此時調用change,定時器回調修改opacity,數(shù)據(jù)修改,模板重新解析,再次調用change。
銷毀流程
解綁(自定義)事件監(jiān)聽器
生命周期
生命周期總結
<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完成模板解析,并把初始的真實的dom元素放入頁面后(掛載完畢),會調用該函數(shù)。 mounted() { this.timer = setInterval(() => { this.opacity -= 0.01; if (this.opacity <= 0) { this.opacity = 1 } }, 16); }, }); </script>
組件化
template:
整個root容器當作模板
會直接替換掉root,把template當作模板進行解析。
非單文件組件
data需要用函數(shù)式寫法
<div id="root"> <h2>{{msg}}</h2> <!--組件標簽--> <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'>點擊提示</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>
組件的幾個注意點
組件的嵌套
<!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'>點擊提示</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
每次調用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"> <!--組件標簽--> <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'>點擊提示</button> </div> `, data() { return { schoolname: "atguigu", schoolage: 20, } }, methods: { show() { console.log(this)//VueComponent實例對象 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);//一個構造函數(shù) console.log(hello);//一個構造函數(shù) console.log(school === hello);//false new Vue({ el: "#root", data: { }, //局部注冊組件 components: { school: school, hello:hello, } }); </script> </body> </html>
Vue實例與組件實例
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
vue-router實現(xiàn)webApp切換頁面動畫效果代碼
本篇文章主要介紹了vue實現(xiàn)app頁面切換效果實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05使用vuetify實現(xiàn)全局v-alert消息通知的方法
使用強大的Vuetify開發(fā)前端頁面,結果發(fā)現(xiàn)官方?jīng)]有提供簡便的全局消息通知組件,所以自己動手寫了一個簡單的組件,接下來通過本文給大家介紹使用vuetify實現(xiàn)全局v-alert消息通知的詳細代碼,感興趣的朋友跟隨小編一起看看吧2024-02-02vue中的rules表單校驗規(guī)則使用方法示例詳解 :rules=“rules“
這篇文章主要介紹了vue中的rules表單校驗規(guī)則使用方法示例詳解 :rules=“rules“,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-11-11