Vue生命周期中的組件化你知道嗎
引出生命周期
此時調用change,定時器回調修改opacity,數據修改,模板重新解析,再次調用change。
銷毀流程
解綁(自定義)事件監聽器
生命周期
生命周期總結
<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元素放入頁面後(掛載完畢),會調用該函數。 mounted() { this.timer = setInterval(() => { this.opacity -= 0.01; if (this.opacity <= 0) { this.opacity = 1 } }, 16); }, }); </script>
組件化
template:
整個root容器當作模板
會直接替換掉root,把template當作模板進行解析。
非單文件組件
data需要用函數式寫法
<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; //創建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); } }, }); //創建stu組件 const student = Vue.extend({ template:` <div> <h2>stuname:{{stuname}}</h2> <h2>stuage{{stuage}}</h2> </div> `, data(){ return{ stuname:'tom', stuage:18, } }, }); //創建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; //創建student組件 const student = Vue.extend({ template:` <div> <h2>stuname:{{stuname}}</h2> <h2>stuage{{stuage}}</h2> </div> `, data(){ return{ stuname:'tom', stuage:18, } }, }); //創建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, } }); //創建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; //創建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); } }, }); //創建hello組件 const hello = Vue.extend({ template: ` <div> <h2>hello:{{hello}}</h2> </div> `, data() { return { hello: "hello", } }, }); console.log(school);//一個構造函數 console.log(hello);//一個構造函數 console.log(school === hello);//false new Vue({ el: "#root", data: { }, //局部註冊組件 components: { school: school, hello:hello, } }); </script> </body> </html>
Vue實例與組件實例
總結
本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!