最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue生命周期中的組件化你知道嗎

 更新時(shí)間:2022年03月08日 15:53:42   作者:BHW1293773843  
這篇文章主要為大家詳細(xì)介紹了Vue生命周期中的組件化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助

引出生命周期

此時(shí)調(diào)用change,定時(shí)器回調(diào)修改opacity,數(shù)據(jù)修改,模板重新解析,再次調(diào)用change。

銷毀流程

解綁(自定義)事件監(jiān)聽(tīng)器

生命周期

生命周期總結(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元素放入頁(yè)面后(掛載完畢),會(huì)調(diào)用該函數(shù)。
            mounted() {
                this.timer = setInterval(() => {
                    this.opacity -= 0.01;
                    if (this.opacity <= 0) { this.opacity = 1 }
                }, 16);
            },
        });
    </script>

組件化 

template:

整個(gè)root容器當(dāng)作模板

會(huì)直接替換掉root,把template當(dāng)作模板進(jìn)行解析。 

 

 非單文件組件

data需要用函數(shù)式寫(xiě)法

<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,
                }
            },
       });
       //全局注冊(cè)組件
       Vue.component('hello',hello);
        new Vue({
            el: "#root",
            data:{
                msg:'this is msg'
            },
            //局部注冊(cè)組件
            components:{
                school:school,
                student,
            }
        });
    </script>

 組件的幾個(gè)注意點(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",
            //局部注冊(cè)組件
            components:{
                app,             
            }         
        });
    </script>
</body>
</html>

 VueComponent

每次調(diào)用extend,都返回了一個(gè)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í)例對(duì)象  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è)構(gòu)造函數(shù)
        console.log(hello);//一個(gè)構(gòu)造函數(shù)
        console.log(school === hello);//false
        new Vue({
            el: "#root",
            data: {
            },
            //局部注冊(cè)組件
            components: {
                school: school,
                hello:hello,
            }
        });
    </script>
</body>
</html>

 Vue實(shí)例與組件實(shí)例

總結(jié)

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!  

相關(guān)文章

  • vue項(xiàng)目前端錯(cuò)誤收集之sentry教程詳解

    vue項(xiàng)目前端錯(cuò)誤收集之sentry教程詳解

    Sentry 是一個(gè)開(kāi)源的錯(cuò)誤追蹤工具,可以幫助開(kāi)發(fā)人員實(shí)時(shí)監(jiān)控和修復(fù)系統(tǒng)中的錯(cuò)誤。這篇文章主要介紹了vue項(xiàng)目前端錯(cuò)誤收集之sentry,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • vue環(huán)形進(jìn)度條組件實(shí)例應(yīng)用

    vue環(huán)形進(jìn)度條組件實(shí)例應(yīng)用

    在本文中我們給大家分享了關(guān)于vue環(huán)形進(jìn)度條組件的使用方法以及實(shí)例代碼,需要的朋友們跟著測(cè)試下吧。
    2018-10-10
  • vue仿網(wǎng)易云音樂(lè)播放器界面的簡(jiǎn)單實(shí)現(xiàn)過(guò)程

    vue仿網(wǎng)易云音樂(lè)播放器界面的簡(jiǎn)單實(shí)現(xiàn)過(guò)程

    興趣乃學(xué)習(xí)的動(dòng)力,想自己動(dòng)手寫(xiě)個(gè)音樂(lè)播放器,查了網(wǎng)上一些博客寫(xiě)了一個(gè),這篇文章主要給大家介紹了關(guān)于vue仿網(wǎng)易云音樂(lè)播放器界面的簡(jiǎn)單實(shí)現(xiàn)過(guò)程,需要的朋友可以參考下
    2021-11-11
  • Django+Vue跨域環(huán)境配置詳解

    Django+Vue跨域環(huán)境配置詳解

    這篇文章主要介紹了Django+Vue跨域環(huán)境配置詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • vue實(shí)現(xiàn)下拉加載其實(shí)沒(méi)那么復(fù)雜

    vue實(shí)現(xiàn)下拉加載其實(shí)沒(méi)那么復(fù)雜

    這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)下拉加載的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • WebStorm啟動(dòng)vue項(xiàng)目報(bào)錯(cuò)代碼:1080?throw?err解決辦法

    WebStorm啟動(dòng)vue項(xiàng)目報(bào)錯(cuò)代碼:1080?throw?err解決辦法

    在使用webstorm新建vue項(xiàng)目時(shí)常會(huì)遇到一些報(bào)錯(cuò),下面這篇文章主要給大家介紹了關(guān)于WebStorm啟動(dòng)vue項(xiàng)目報(bào)錯(cuò)代碼:1080?throw?err的解決辦法,文中將解決辦法介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • vue實(shí)現(xiàn)登錄類型切換

    vue實(shí)現(xiàn)登錄類型切換

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)登錄類型切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • vue使用video插件vue-video-player詳解

    vue使用video插件vue-video-player詳解

    這篇文章主要為大家詳細(xì)介紹了vue使用video插件vue-video-player,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • 基于vue的換膚功能的示例代碼

    基于vue的換膚功能的示例代碼

    本篇文章主要介紹了基于vue的換膚功能的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • Vue 2.0在IE11中打開(kāi)項(xiàng)目頁(yè)面空白的問(wèn)題解決

    Vue 2.0在IE11中打開(kāi)項(xiàng)目頁(yè)面空白的問(wèn)題解決

    這篇文章主要給大家介紹了關(guān)于Vue 2.0在ie 11中打開(kāi)項(xiàng)目頁(yè)面空白問(wèn)題的解決方法,文中詳細(xì)分析出現(xiàn)該問(wèn)題的原因,并給出了詳細(xì)的解決方法,需要的朋友可以參考借鑒,下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07

最新評(píng)論

腾冲县| 双流县| 府谷县| 漳平市| 增城市| 镇安县| 泗水县| 山东省| 盘山县| 武义县| 漳平市| 鸡东县| 开化县| 石狮市| 象山县| 延吉市| 临海市| 文安县| 武强县| 五寨县| 河津市| 湟中县| 韶关市| 明光市| 伊通| 墨竹工卡县| 漳浦县| 阳泉市| 花莲市| 黑水县| 怀远县| 论坛| 定西市| 中牟县| 江华| 兴国县| 行唐县| 溆浦县| 陕西省| 堆龙德庆县| 晋城|