Day03Code

湯小洋Vue課程代碼記錄
課程連接地址:http://edu.51cto.com/course/10543.html
抄錄至簡書僅方便自己查閱,小伙伴們可以去學(xué)院支持該課程

code01

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定義組件的兩種方式</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="itany">
        <hello></hello>
        <my-world></my-world>
    </div>

    <script>
        /**
         * 方式1:先創(chuàng)建組件構(gòu)造器回铛,然后由組件構(gòu)造器創(chuàng)建組件
         */
        //1.使用Vue.extend()創(chuàng)建一個(gè)組件構(gòu)造器
        var MyComponent=Vue.extend({
            template:'<h3>Hello World</h3>'
        });
        //2.使用Vue.component(標(biāo)簽名,組件構(gòu)造器)狗准,根據(jù)組件構(gòu)造器來創(chuàng)建組件
        Vue.component('hello',MyComponent);
        
        /**
         * 方式2:直接創(chuàng)建組件(推薦)
         */
        // Vue.component('world',{
        Vue.component('my-world',{
            template:'<h1>你好,世界</h1>'
        });

        var vm=new Vue({ //這里的vm也是一個(gè)組件腔长,稱為根組件Root
            el:'#itany',
            data:{
                msg:'網(wǎng)博'
            }
        }); 
    </script>
</body>
</html>

code02

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>組件的分類</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="itany">
        <my-hello></my-hello>
        <my-world></my-world>
    </div>

    <script>
        /**
         * 全局組件,可以在所有vue實(shí)例中使用
         */
        Vue.component('my-hello',{
            template:'<h3>{{name}}</h3>',
            data:function(){ //在組件中存儲(chǔ)數(shù)據(jù)時(shí)验残,必須以函數(shù)形式镣丑,函數(shù)返回一個(gè)對(duì)象
                return {
                    name:'alice'
                }
            }
        });

        /**
         * 局部組件舔糖,只能在當(dāng)前vue實(shí)例中使用
         */
        var vm=new Vue({
            el:'#itany',
            data:{
                name:'tom'
            },
            components:{ //局部組件
                'my-world':{
                    template:'<h3>{{age}}</h3>',
                    data(){
                        return {
                            age:25
                        }
                    }
                }
            }
        }); 
    </script>
</body>
</html>

code03

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>引用模板</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="itany">
        <my-hello></my-hello>
        <my-hello></my-hello>
    </div>

    <template id="wbs">
        <!-- <template>必須有且只有一個(gè)根元素 -->
        <div>
            <h3>{{msg}}</h3>
            <ul>
                <li v-for="value in arr">{{value}}</li>
            </ul>
        </div>
    </template>

    <script>
        var vm=new Vue({
            el:'#itany',
            components:{
                'my-hello':{
                    name:'wbs17022',  //指定組件的名稱,默認(rèn)為標(biāo)簽名莺匠,可以不設(shè)置
                    template:'#wbs',
                    data(){
                        return {
                            msg:'歡迎來到南京網(wǎng)博',
                            arr:['tom','jack','mike']
                        }
                    }
                }
                
            }
        }); 
    </script>
</body>
</html>

code04

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>動(dòng)態(tài)組件</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="itany">
        <button @click="flag='my-hello'">顯示hello組件</button>
        <button @click="flag='my-world'">顯示world組件</button>


        <div>
            <!-- 使用keep-alive組件緩存非活動(dòng)組件金吗,可以保留狀態(tài),避免重新渲染趣竣,默認(rèn)每次都會(huì)銷毀非活動(dòng)組件并重新創(chuàng)建 -->
            <keep-alive>
                <component :is="flag"></component>  
            </keep-alive>
        </div>
    </div>

    <script>
        var vm=new Vue({
            el:'#itany',
            data:{
                flag:'my-hello'
            },
            components:{
                'my-hello':{
                    template:'<h3>我是hello組件:{{x}}</h3>',
                    data(){
                        return {
                            x:Math.random()
                        }
                    }
                },
                'my-world':{
                    template:'<h3>我是world組件:{{y}}</h3>',
                    data(){
                        return {
                            y:Math.random()
                        }
                    }
                }
            }
        }); 
    </script>
</body>
</html>

code05

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>父子組件及組件間數(shù)據(jù)傳遞</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="itany">
        <my-hello></my-hello>
    </div>
    
    <template id="hello">
        <div>
            <h3>我是hello父組件</h3>
            <h3>訪問自己的數(shù)據(jù):{{msg}},{{name}},{{age}},{{user.username}}</h3>
            <h3>訪問子組件的數(shù)據(jù):{{sex}},{{height}}</h3>
            <hr>

            <my-world :message="msg" :name="name" :age="age" @e-world="getData"></my-world>
        </div>
    </template>

    <template id="world">
        <div>
            <h4>我是world子組件</h4>
            <h4>訪問父組件中的數(shù)據(jù):{{message}},{{name}},{{age}},{{user.username}}</h4>
            <h4>訪問自己的數(shù)據(jù):{{sex}},{{height}}</h4>
            <button @click="send">將子組件的數(shù)據(jù)向上傳遞給父組件</button>
        </div>
    </template>

    <script>
        var vm=new Vue({ //根組件
            el:'#itany',
            components:{
                'my-hello':{  //父組件
                    methods:{
                        getData(sex,height){
                            this.sex=sex;
                            this.height=height;
                        }
                    },
                    data(){
                        return {
                            msg:'網(wǎng)博',
                            name:'tom',
                            age:23,
                            user:{id:9527,username:'唐伯虎'},
                            sex:'',
                            height:''
                        }
                    },
                    template:'#hello',
                    components:{
                        'my-world':{ //子組件
                            data(){
                                return {
                                    sex:'male',
                                    height:180.5
                                }
                            },
                            template:'#world',
                            // props:['message','name','age','user'] //簡單的字符串?dāng)?shù)組
                            props:{ //也可以是對(duì)象摇庙,允許配置高級(jí)設(shè)置,如類型判斷期贫、數(shù)據(jù)校驗(yàn)跟匆、設(shè)置默認(rèn)值
                                message:String,
                                name:{
                                    type:String,
                                    required:true
                                },
                                age:{
                                    type:Number,
                                    default:18,
                                    validator:function(value){
                                        return value>=0;
                                    }
                                },
                                user:{
                                    type:Object,
                                    default:function(){ //對(duì)象或數(shù)組的默認(rèn)值必須使用函數(shù)的形式來返回
                                        return {id:3306,username:'秋香'};
                                    }
                                }
                            },
                            methods:{
                                send(){
                                    // console.log(this);  //此處的this表示當(dāng)前子組件實(shí)例
                                    this.$emit('e-world',this.sex,this.height); //使用$emit()觸發(fā)一個(gè)事件,發(fā)送數(shù)據(jù)
                                }
                            }
                        }
                    }
                }
            }
        }); 
    </script>
</body>
</html>

code06

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>單向數(shù)據(jù)流</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="itany">
        <h2>父組件:{{name}}</h2>
        <input type="text" v-model="name">
        <h2>父組件:{{user.age}}</h2>

        <hr>

        <my-hello :name.sync="name" :user="user"></my-hello>
    </div>
    
    <template id="hello">
        <div>
            <h3>子組件:{{name}}</h3>
            <h3>子組件:{{user.age}}</h3>
            <button @click="change">修改數(shù)據(jù)</button>
        </div>
    </template>

    <script>
        var vm=new Vue({ //父組件
            el:'#itany',
            data:{
                name:'tom',
                user:{
                    name:'zhangsan',
                    age:24
                }
            },
            components:{
                'my-hello':{ //子組件
                    template:'#hello',
                    props:['name','user'],
                    data(){
                        return {
                            username:this.name //方式1:將數(shù)據(jù)存入另一個(gè)變量中再操作
                        }
                    },
                    methods:{
                        change(){
                            // this.username='alice';
                            // this.name='alice';
                            // this.$emit('update:name','alice'); //方式2:a.使用.sync通砍,需要顯式地觸發(fā)一個(gè)更新事件
                            this.user.age=18;
                        }
                    }
                }
            }
        }); 
    </script>
</body>
</html>

code07

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>非父子組件間的通信</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="itany">
        <my-a></my-a>
        <my-b></my-b>
        <my-c></my-c>
    </div>

    <template id="a">
        <div>
            <h3>A組件:{{name}}</h3>
            <button @click="send">將數(shù)據(jù)發(fā)送給C組件</button>
        </div>
    </template>

    <template id="b">
        <div>
            <h3>B組件:{{age}}</h3>
            <button @click="send">將數(shù)組發(fā)送給C組件</button>
        </div>
    </template>
    
    <template id="c">
        <div>
            <h3>C組件:{{name}}玛臂,{{age}}</h3>
        </div>
    </template>

    <script>
        //定義一個(gè)空的Vue實(shí)例
        var Event=new Vue();

        var A={
            template:'#a',
            data(){
                return {
                    name:'tom'
                }
            },
            methods:{
                send(){
                    Event.$emit('data-a',this.name);
                }
            }
        }
        var B={
            template:'#b',
            data(){
                return {
                    age:20
                }
            },
            methods:{
                send(){
                    Event.$emit('data-b',this.age);
                }
            }
        }
        var C={
            template:'#c',
            data(){
                return {
                    name:'',
                    age:''
                }
            },
            mounted(){ //在模板編譯完成后執(zhí)行
                Event.$on('data-a',name => {
                    this.name=name;
                    // console.log(this);
                });

                Event.$on('data-b',age => {
                    this.age=age;
                });
            }
        }

        var vm=new Vue({
            el:'#itany',
            components:{
                'my-a':A,
                'my-b':B,
                'my-c':C
            }
        }); 
    </script>
</body>
</html>

code08

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>slot內(nèi)容分發(fā)</title>
    <script src="js/vue.js"></script>
</head>
<body>
    <div id="itany">
        <!-- <my-hello>wbs17022</my-hello> -->
        <my-hello>
            <ul slot="s1">
                <li>aaa</li>
                <li>bbb</li>
                <li>ccc</li>
            </ul>
            <ol slot="s2">
                <li>111</li>
                <li>222</li>
                <li>333</li>
            </ol>
        </my-hello>
    </div>

    <template id="hello">
        <div>
            <slot name="s2"></slot>
            <h3>welcome to itany</h3>
            <!-- <slot>如果沒有原內(nèi)容烤蜕,則顯示該內(nèi)容</slot> -->
            <slot name="s1"></slot>
        </div>
    </template>

    <script>

        var vm=new Vue({
            el:'#itany',
            components:{
                'my-hello':{
                    template:'#hello'
                }
            }
        }); 
    </script>
</body>
</html>

code09

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>路由基本用法</title>
    <style>
        /* .router-link-active{
            font-size:20px;
            color:#ff7300;
            text-decoration:none;
        } */
        .active{
            font-size:20px;
            color:#ff7300;
            text-decoration:none;
        }
    </style>
    <script src="js/vue.js"></script>
    <script src="js/vue-router.js"></script>
</head>
<body>
    <div id="itany">
        <div>
            <!-- 使用router-link組件來定義導(dǎo)航,to屬性指定鏈接url -->
            <router-link to="/home">主頁</router-link>
            <router-link to="/news">新聞</router-link>
        </div>
        <div>
            <!-- router-view用來顯示路由內(nèi)容 -->
            <router-view></router-view>
        </div>
    </div>

    <script>
        //1.定義組件
        var Home={
            template:'<h3>我是主頁</h3>'
        }
        var News={
            template:'<h3>我是新聞</h3>'
        }

        //2.配置路由
        const routes=[
            {path:'/home',component:Home},
            {path:'/news',component:News},
            {path:'*',redirect:'/home'} //重定向
        ]

        //3.創(chuàng)建路由實(shí)例
        const router=new VueRouter({
            routes, //簡寫迹冤,相當(dāng)于routes:routes
            // mode:'history', //更改模式
            linkActiveClass:'active' //更新活動(dòng)鏈接的class類名
        });

        //4.創(chuàng)建根實(shí)例并將路由掛載到Vue實(shí)例上
        new Vue({
            el:'#itany',
            router //注入路由
        });
    </script>
</body>
</html>

code10

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>路由嵌套和參數(shù)傳遞</title>
    <link rel="stylesheet" href="css/animate.css">
    <style>
        .active{
            font-size:20px;
            color:#ff7300;
            text-decoration:none;
        }
    </style>
    <script src="js/vue.js"></script>
    <script src="js/vue-router.js"></script>
</head>
<body>
    <div id="itany">
        <div>
            <router-link to="/home">主頁</router-link>
            <router-link to="/user">用戶</router-link>
        </div>
        <div>
            <transition enter-active-class="animated bounceInLeft" leave-active-class="animated bounceOutRight">
                <router-view></router-view>
            </transition>
        </div>

        <hr>
        <button @click="push">添加路由</button>
        <button @click="replace">替換路由</button>
    </div>

    <template id="user">
        <div>
            <h3>用戶信息</h3>
            <ul>
                <router-link to="/user/login?name=tom&pwd=123" tag="li">用戶登陸</router-link>
                <router-link to="/user/regist/alice/456" tag="li">用戶注冊(cè)</router-link>
            </ul>
            <router-view></router-view>
        </div>
    </template>

    <script>
        var Home={
            template:'<h3>我是主頁</h3>'
        }
        var User={
            template:'#user'
        }
        var Login={
            template:'<h4>用戶登陸讽营。。泡徙。獲取參數(shù):{{$route.query}},{{$route.path}}</h4>'
        }
        var Regist={
            template:'<h4>用戶注冊(cè)橱鹏。。堪藐。獲取參數(shù):{{$route.params}},{{$route.path}}</h4>'
        }

        const routes=[
            {
                path:'/home',
                component:Home
            },
            {
                path:'/user',
                component:User,
                children:[
                    {
                        path:'login',
                        component:Login
                    },
                    {
                        path:'regist/:username/:password',
                        component:Regist
                    }
                ]
            },
            {
                path:'*',
                redirect:'/home'
            }
        ]

        const router=new VueRouter({
            routes, //簡寫莉兰,相當(dāng)于routes:routes
            linkActiveClass:'active' //更新活動(dòng)鏈接的class類名
        });

        new Vue({
            el:'#itany',
            router, //注入路由
            methods:{
                push(){
                    router.push({path:'home'}); //添加路由,切換路由
                },
                replace(){
                    router.replace({path:'user'}); //替換路由礁竞,沒有歷史記錄
                }
            }
        });
    </script>
</body>
</html>

code11


code12


code13


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末糖荒,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子模捂,更是在濱河造成了極大的恐慌捶朵,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,366評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件狂男,死亡現(xiàn)場(chǎng)離奇詭異综看,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)岖食,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門红碑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人泡垃,你說我怎么就攤上這事句喷。” “怎么了兔毙?”我有些...
    開封第一講書人閱讀 165,689評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長兄春。 經(jīng)常有香客問我澎剥,道長,這世上最難降的妖魔是什么赶舆? 我笑而不...
    開封第一講書人閱讀 58,925評(píng)論 1 295
  • 正文 為了忘掉前任哑姚,我火速辦了婚禮,結(jié)果婚禮上芜茵,老公的妹妹穿的比我還像新娘叙量。我一直安慰自己,他們只是感情好九串,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,942評(píng)論 6 392
  • 文/花漫 我一把揭開白布绞佩。 她就那樣靜靜地躺著寺鸥,像睡著了一般。 火紅的嫁衣襯著肌膚如雪品山。 梳的紋絲不亂的頭發(fā)上胆建,一...
    開封第一講書人閱讀 51,727評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音肘交,去河邊找鬼笆载。 笑死,一個(gè)胖子當(dāng)著我的面吹牛涯呻,可吹牛的內(nèi)容都是我干的凉驻。 我是一名探鬼主播,決...
    沈念sama閱讀 40,447評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼复罐,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼涝登!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起市栗,我...
    開封第一講書人閱讀 39,349評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤缀拭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后填帽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蛛淋,經(jīng)...
    沈念sama閱讀 45,820評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,990評(píng)論 3 337
  • 正文 我和宋清朗相戀三年篡腌,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了褐荷。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,127評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡嘹悼,死狀恐怖叛甫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情杨伙,我是刑警寧澤其监,帶...
    沈念sama閱讀 35,812評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站限匣,受9級(jí)特大地震影響抖苦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜米死,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,471評(píng)論 3 331
  • 文/蒙蒙 一锌历、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧峦筒,春花似錦究西、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽遮斥。三九已至,卻和暖如春商膊,著一層夾襖步出監(jiān)牢的瞬間伏伐,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評(píng)論 1 272
  • 我被黑心中介騙來泰國打工晕拆, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留藐翎,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,388評(píng)論 3 373
  • 正文 我出身青樓实幕,卻偏偏與公主長得像吝镣,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子昆庇,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,066評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 作為一名合格的IT人,這些課程你肯定不會(huì)錯(cuò)過表蝙,今天給大家來一個(gè)終極揭秘版100個(gè)精品教程拴测,希望對(duì)大家有幫助~4.2...
    qingfengxulai閱讀 2,017評(píng)論 1 6
  • 湯小洋Vue課程代碼記錄課程連接地址:http://edu.51cto.com/course/10543.html...
    鳥它鳥閱讀 314評(píng)論 0 1
  • 湯小洋Vue課程代碼記錄課程連接地址:http://edu.51cto.com/course/10543.html...
    鳥它鳥閱讀 530評(píng)論 0 1
  • 今天和彤寶在外面種花的地方玩,她突然急急忙忙叫:我要尿尿府蛇,我要尿尿集索。我讓她就蹲在花旁邊尿。尿完她說汇跨,像蟲子在游游游...
    心遙遠(yuǎn)閱讀 187評(píng)論 0 0
  • Robert Henri 寫的藝術(shù)精神這本書分了5個(gè)部分务荆。美始于感動(dòng),我們所畫之物穷遂,要是使我們感動(dòng)的函匕,使我們...
    簡拾閱讀 189評(píng)論 0 0