首頁 > 軟體

Vue生命週期中的元件化你知道嗎

2022-03-08 19:00:52

引出生命週期

此時呼叫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範例與元件範例

總結

本篇文章就到這裡了,希望能夠給你帶來幫助,也希望您能夠多多關注it145.com的更多內容!  


IT145.com E-mail:sddin#qq.com