vue1.0與2.0的區(qū)別

本文的意義在于總結(jié)一些因為版本的更迭導致比較常見的地方引起錯誤锋勺,具體說明可以參考vue官網(wǎng)

目錄

1沙庐、組件模板寫法
2、組件定義
3咸这、生命周期
4夷恍、重復數(shù)據(jù)循環(huán)
5、自定義鍵盤指令
6媳维、filter過濾器
7酿雪、組件通信
8、動畫
9侄刽、vue-router

1执虹、組件模板寫法變更

vue2.0不在支持片段代碼,例如以下

//下邊的組件寫法在vue1.0可以正常執(zhí)行唠梨,但vue2.0會給出警告袋励,必須給他一個根節(jié)點,也就是唯一父級
//[Vue warn]: Component template should contain exactly one root element:
<script>
Vue.component('my-aaa',{
    template:'<h3>我是組件</h3><strong>我是加粗標簽</strong>'
});
</script>

//推薦此種方式寫組件当叭,必須有根元素茬故,包裹住所有的代碼
<script>
Vue.component('my-aaa',{
    template:'#aaa'
});
</script>
<body>
    <template id="aaa">
        <div>
            <h3>我是組件</h3>
            <strong>我是加粗標簽</strong>
        </div>
    </template>
</body>
2、組件定義
//使用組件必須注冊
//[Vue warn]: Unknown custom element: <my-aaa> - did you register the component correctly? For recursive components, make sure to provide the "name" option. 
<script>
    var Home={  //這是2.0組件
        template:'#aaa'
    };  //Vue.extend()
    Vue.component('my-aaa',Home);  //注冊方式1
</script>
<body>
    <template id="aaa">
        <div>
            <h3>我是組件</h3>
            <strong>我是加粗標簽</strong>
        </div>
    </template>
    <div id="box">
        <my-aaa></my-aaa>
    </div>
</body>
//第二種方式
<script>
    var Home={  //這是2.0組件
        template:'#aaa'
    };  //Vue.extend()
    window.onload=function(){
        new Vue({
            el:'#box',
            components:{  //注冊方式2
                'aaa':Home
            }
        });
    };
</script>
<body>
    <template id="aaa">
        <div>
            <h3>我是組件</h3>
            <strong>我是加粗標簽</strong>
        </div>
    </template>
    <div id="box">
        <my-aaa></my-aaa>
    </div>
</body>
3蚁鳖、生命周期
vue1.0生命周期:
        init    
        created
        beforeCompile
        compiled
        ready       √   ->     mounted
        beforeDestroy   
        destroyed
vue2.0生命周期:
        beforeCreate    組件實例剛剛被創(chuàng)建,屬性都沒有
        created 實例已經(jīng)創(chuàng)建完成磺芭,屬性已經(jīng)綁定
        beforeMount 模板編譯之前
        mounted 模板編譯之后,代替之前ready  *
        beforeUpdate    組件更新之前
        updated 組件更新完畢  *
        beforeDestroy   組件銷毀前
        destroyed   組件銷毀后

<script>
    new Vue({
        el:'#box',
        beforeCreate(){
            console.log('組件實例剛剛被創(chuàng)建');
        },
        created(){
            console.log('實例已經(jīng)創(chuàng)建完成');
        },
        beforeMount(){
            console.log('模板編譯之前');
        },
        mounted(){
            console.log('模板編譯完成');
        },
        beforeUpdate(){
            console.log('組件更新之前');
        },
        updated(){
            console.log('組件更新完畢');
        },
        beforeDestroy(){
            console.log('組件銷毀之前');
        },
        destroyed(){
            console.log('組件銷毀之后');
        }
    });
</script>
4醉箕、重復數(shù)據(jù)循環(huán)
//[Vue warn]: Duplicate value found in v-for="val in list": "background". Use track-by="$index" if you are expecting duplicate values.
//vue1.0循環(huán)重復數(shù)據(jù)會受限制钾腺,vue2.0正常
<script>
    window.onload=function(){
        new Vue({
            el:'#box',
            data:{
                list:['width','height','border']
            },
            methods:{
                add(){
                    this.list.push('background');
                }
            }
        });
    };
</script>
<div id="box">
    <input type="button" value="添加" @click="add">
    <ul>
        <li v-for="val in list">  //vue1.0循環(huán)需要添加 track-by="$index"
            {{val}}
        </li>
//[Vue warn]: Property or method "$key" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. 
        <li v-for="val in list">  
            {{$index}}  {{$key}} //vue2.0去掉了隱式一些變量 $index   $key
        </li>
        <li v-for="(index,val) in list">  //vue1.0循環(huán)寫法v-for="(index,val) in array"
            {{val}} {{index}}
        </li>
        <li v-for="(val,index) in list">  //vue2.0循環(huán)寫法 v-for="(index,val) in array"
            {{val}} {{index}}                 // 傾向于js原生forEach徙垫,先寫val再寫index
        </li>
    </ul>
</div>

//提高循環(huán)性能 track-by="id" 變成 :key="index"
<script>
    window.onload=function(){
        new Vue({
            el:'#box',
            data:{
                list:{
                    a:'apple',
                    b:'banana',
                    c:'cell'
                }
            }
        });
    };
</script>
<div id="box">
    <ul>
        <li v-for="(val,key) in list" :key="index">
            {{val}} {{key}}
        </li>
    </ul>
</div>
5、自定義鍵盤指令
<script>
    window.onload=function(){
        new Vue({
            el:'#box',
            data:{
            },
            methods:{
                change(){
                    alert('改變了');
                }
            }
        });
        //Vue.directive('on').keyCodes.ctrl=17;    //vue1.0寫法
        Vue.config.keyCodes.ctrl=17;                 //vue2.0寫法
    };
</script>
<div id="box">
    <input type="text" @keyup.ctrl="change">
</div>
6放棒、filter過濾器

vue1.0系統(tǒng)就自帶很多過濾姻报,例如currency/json等等。到了vue2.0间螟,作者刪除全部內(nèi)置過濾器吴旋,保留了自定義過濾器,但自定義過濾器傳參格式有點區(qū)別厢破。

<script>
    Vue.filter('toDou',function(n,a,b){
        alert(a+','+b);
        return n<10?'0'+n:''+n;
    });
    window.onload=function(){
        new Vue({
            el:'#box',
            data:{
                msg:9
            }
        });
    };
</script>
<div id="box">
    {{msg | toDou '12' '5'}}   //vue1.0寫法
    {{msg | toDou('12','5')}}  //vue2.0寫法
</div>
7荣瑟、組件通信

子級獲取父級的數(shù)據(jù),通過props

//vue1.0寫法摩泪,子組件可以更改父組件信息笆焰,可以是同步sync
<script>
    window.onload=function(){
        new Vue({
            el:'#box',
            data:{
                a:'我是父組件數(shù)據(jù)'
            },
            components:{
                'child-com':{
                    props:['msg'],
                    template:'#child',
                    methods:{
                        change(){
                            this.msg='被更改了'
                        }
                    }
                }
            }
        });
    };
</script>
<template id="child">
    <div>
        <span>我是子組件</span>
        <input type="button" value="按鈕" @click="change">
        <strong>{{msg}}</strong>
    </div>
</template>
<div id="box">
    父級: ->{{a}}
    <br>
    <child-com :msg.sync="a"></child-com>  //需要同步更改父級需要加上.sync
</div>

//[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "msg" 
//vue2.0寫法跟vue1.0有點區(qū)別,不然會報以上警告见坑。 不允許直接給父級的數(shù)據(jù)仙辟,做賦值操作
//處理方式 a). 父組件每次傳一個對象給子組件, 對象之間引用,推薦  b). 只是不報錯, mounted中轉(zhuǎn)
//方式a
<script>
    window.onload=function(){
        new Vue({
            el:'#box',
            data:{
                giveData:{
                    a:'我是父組件數(shù)據(jù)'
                }
            },
            components:{
                'child-com':{
                    props:['msg'],
                    template:'#child',
                    methods:{
                        change(){
                            this.msg.a='被改了';
                        }
                    }
                }
            }
        });
    };
</script>
<template id="child">
    <div>
        <span>我是子組件</span>
        <input type="button" value="按鈕" @click="change">
        <strong>{{msg.a}}</strong>
    </div>
</template>
<div id="box">
    父級: ->{{giveData.a}}
    <br>
    <child-com :msg="giveData"></child-com>
</div>
//方式b
<script>
    window.onload=function(){
        new Vue({
            el:'#box',
            data:{
                a:'我是父組件數(shù)據(jù)'
            },
            components:{
                'child-com':{
                    data(){
                        return {
                            b:''
                        }
                    },
                    props:['msg'],
                    template:'#child',
                    mounted(){
                        this.b=this.msg;
                    },
                    methods:{
                        change(){
                            this.b='被改了';
                        }
                    }
                }
            }
        });
    };
</script>
<template id="child">
    <div>
        <span>我是子組件</span>
        <input type="button" value="按鈕" @click="change">
        <strong>{鳄梅}</strong>
    </div>
</template>

<div id="box">
    父級: ->{{a}}
    <br>
    <child-com :msg.sync="a"></child-com>
</div>

//注意如果是父子級可以通過以上方式處理叠国。如果是同級組件之間想傳遞數(shù)據(jù),也就是單一事件管理戴尸,通過vm.$emit()傳遞粟焊,vm.$on()接收。類似vuex實現(xiàn)原理孙蒙。
格式參考:
var Event=new Vue();
Event.$emit(事件名稱, 數(shù)據(jù))
Event.$on(事件名稱,function(data){
    //data
}.bind(this));
//具體實現(xiàn)
<script>
    //準備一個空的實例對象
    var Event=new Vue();
    var A={
        template:`
            <div>
                <span>我是A組件</span> -> {{a}}
                <input type="button" value="把A數(shù)據(jù)給C" @click="send">
            </div>
            `,
        methods:{
            send(){
                Event.$emit('a-msg',this.a);
            }
        },
        data(){
            return {
                a:'我是a數(shù)據(jù)'
            }
        }
    };
    var B={
        template:`
            <div>
                <span>我是B組件</span> -> {{a}}
                <input type="button" value="把B數(shù)據(jù)給C">
            </div>
            `,
        data(){
            return {
                a:'我是b數(shù)據(jù)'
            }
        }
    };
    var C={
        template:`
            <div>
                <h3>我是C組件</h3>
                <span>接收過來的A的數(shù)據(jù)為: {{a}}</span>
            </div>
            `,
        data(){
            return {
                a:'',
                b:''
            }
        },
        mounted(){
            //var _this=this;
            Event.$on('a-msg',function(a){
                this.a=a;
            }.bind(this));
        }
    };
    window.onload=function(){
        new Vue({
            el:'#box',
            components:{
                'com-a':A,
                'com-b':B,
                'com-c':C
            }
        });
    };
</script>
<div id="box">
    <com-a></com-a>
    <com-b></com-b>
    <com-c></com-c>
</div>
8项棠、動畫

動畫如果有結(jié)合animate,相關配置參數(shù)和效果查看可以參考animate官網(wǎng)

vue1.0寫法,transition是屬性
    <p transition="fade"></p>
處理好fade相關樣式就可以實現(xiàn)動畫效果
.fade-transition{}
.fade-enter{}
.fade-leave{}

vue2.0寫法挎峦,transition變成了組件香追,需要把要做動畫的包起來,一般是包router-view
<transition name="fade">
    運動東西(元素坦胶,屬性透典、路由....)
</transition>
class定義:
.fade-enter-active,.fade-leave-active{transition:1s all ease;}
.fade-enter{}   //初始狀態(tài)
.fade-enter-active{}  //變化成什么樣  ->  當元素出來(顯示)
.fade-leave{}
.fade-leave-active{} //變成成什么樣   -> 當元素離開(消失)

動畫可以配合animate.css使用,把fade和name去掉顿苇。給transition加enter-active-class="zoomInleft" leave-active-class="zoomOutRight",給運動的元素本身加class="animated"峭咒。 
<transition enter-active-class="animated zoomInLeft" leave-active-class="animated zoomOutRight">
    <p v-show="show"></p>
</transition>

多個元素運動需要使用transition-group,并且把每個運動元素加個:key="index":
<transition-group enter-active-class="" leave-active-class="">
    <p :key=""></p>
    <p :key=""></p>
</transition-group>
9纪岁、vue-router
vue1.0寫法:
    <a v-link="{path:'/home'}">我是主頁</a>
    router.rediect   //重定向  2.0廢棄了
    subRoutes   //路由嵌套用subRoutes凑队,配置跟父級一樣是json
vue2.0寫法:
    <router-link to="/home">我是主頁</router-link>  它會自動解析成a v-link形式
    const routes = [   //配置路由
    {path:'/home',component:Home},
    {path:'*',redirect:'/home'}   //重定向
    ...一個個json
    ];
    children   //路由嵌套用children幔翰,配置跟父級一樣是json

路由實例方法:
    router.push({path:'home'});  //直接添加一個路由,表現(xiàn)切換路由漩氨,本質(zhì)往歷史記錄里面添加一個
    router.replace({path:'news'}) //替換路由西壮,不會往歷史記錄里面添加

帶有參數(shù)的路由配置
<ul>
    <li><router-link to="/user/strive/age/10">Strive</router-link></li>
    <li><router-link to="/user/blue/age/80">Blue</router-link></li>
    <li><router-link to="/user/eric/age/70">Eric</router-link></li>
</ul>
//配置路由
const routes=[
    {path:'/home', component:Home},
    {
        path:'/user',
        component:User,
        children:[
            {path:':username/age/:age', component:UserDetail}
        ]
    },
    {path:'*', redirect:'/home'}  //找不到的頁面全部重定向到home,相當于404
];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市叫惊,隨后出現(xiàn)的幾起案子款青,更是在濱河造成了極大的恐慌,老刑警劉巖赋访,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異缓待,居然都是意外死亡蚓耽,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門旋炒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來步悠,“玉大人,你說我怎么就攤上這事瘫镇《κ蓿” “怎么了?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵铣除,是天一觀的道長谚咬。 經(jīng)常有香客問我,道長尚粘,這世上最難降的妖魔是什么择卦? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮郎嫁,結(jié)果婚禮上秉继,老公的妹妹穿的比我還像新娘。我一直安慰自己泽铛,他們只是感情好尚辑,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著盔腔,像睡著了一般杠茬。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上弛随,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天澈蝙,我揣著相機與錄音,去河邊找鬼撵幽。 笑死灯荧,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的盐杂。 我是一名探鬼主播逗载,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼哆窿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了厉斟?” 一聲冷哼從身側(cè)響起挚躯,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎擦秽,沒想到半個月后码荔,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡感挥,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年缩搅,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片触幼。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡硼瓣,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出置谦,到底是詐尸還是另有隱情堂鲤,我是刑警寧澤,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布媒峡,位于F島的核電站瘟栖,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏谅阿。R本人自食惡果不足惜慢宗,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望奔穿。 院中可真熱鬧镜沽,春花似錦、人聲如沸贱田。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽男摧。三九已至蔬墩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間耗拓,已是汗流浹背拇颅。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留乔询,地道東北人樟插。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親黄锤。 傳聞我的和親對象是個殘疾皇子搪缨,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355

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