vue2与vue1的区别

区别1:

  在vue2中使用v-for指令时它可以添加重复的内容,就像可以添加相同的留言内容,下面我们来看一下

  在写代码的时候首先要引入的是vue2js文件。

  html代码:  

<div id="box">
        <input type="button" value="添加" @click="add()">
        <ul>
            <li v-for="item in arr">{{item}}</li>
        </ul>
    </div>

 

  js代码:

    

1

2

3

4

5

6

7

8

9

10

11

12

13

window.onload=function () {

           new Vue({

               el:"#box",

               data:{

                  arr:[1,2,3,4,5,6]

               },

               methods: {

                   add:function () {

                       this.arr.unshift("1")

                   }

               }

           })

       }

  但是,还有一点不同的地方就是没有$index了,在vue1中是有的,但我们可以手动添加$index

  

1

2

3

4

5

6

<div id="box">

        <input type="button" value="添加" @click="add()">

        <ul>

            <li v-for="(val,index) in arr">{{val}}------->{{index}}</li>

        </ul>

    </div>

 区别2:

    我们在vue2中跟vue1中有一个很大的区别就是没有过滤器!!!我们用着过滤器的时候要要自己定义。

 区别3:

      再者我们在使用组件之间的通讯时也不同,下面我们来看一下:

 html代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

<div id="div">

    我是父组件---->{{emitData.msg}}<br>

    <child-com :m="emitData"></child-com>

</div>

</body>

</html>

<template id="tpl">

    <div>

        <span>我是子组件----></span>

        {{m.msg}}<br>

        <input type="button" value="change" @click="change()"/>

    </div>

</template>

  js代码:

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

window.onload = function(){

       new Vue({

           el:"#div",

           data:{

               emitData:{  //写为json 原理:js中对象的引用

                   msg:"我是父组件数据"

               }

           },

           components:{

               'child-com':{

                   props:['m'],

                   template:"#tpl",

                   methods:{

                       change(){

                           this.m.msg='变了';

                       }

                   }

               }

           }

       })

   }

 这不是vue2中的方法但是我们可以使用这种方法来解决问题。 

区别4: 

  有一个最基本的区别就是我们在定义模板的时应该把模板的东西用一个大盒子包起来。

1

2

3

<template id="tpl">

    <div><h3>3333333</h3><strong>strong</strong></div>

</template>

 区别5: 

    对于生命周期也是有所不同的,我们vue2中的生命周期是这样的

    

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

window.onload=function () {

           new Vue({

               el:"#box",

               data:{

                   msg:"lalalal"

               },

               beforeCreate () {

                   alert("实例创建之前")

               },

               created() {

                   alert("实例创建完成")

               },

               beforeMount() {

                   alert("数据编译之前")

               },

               mounted() {

                   alert("数据编译完成")

               },

               beforeUpdate:function () {

                   console.log("数据更新之前")

               },

               updated:function () {

                   console.log("数据解析完成")

               },

               beforeDestroy:function () {

                   alert("数据销毁之前")

               },

               destroyed:function () {

                   alert("数据销毁完成")

               }

           })

       }

  最后我们来看一下单一事件中管理组件通讯

html:

1

2

3

4

5

<div id="div">

    <com-a></com-a>

    <com-b></com-b>

    <com-c></com-c>

</div>

  js代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

<script>

    window.onload = function(){

        const event=new Vue;

        const A={

            template:`

            <div>

                <span>我是A组件---------></span>{{msg1}}

                <input type="button" value="把a组件的数据传给c" @click="send()">

            </div>

            `,

            data(){

                return{

                    msg1:"我是A组件的数据"

                }

            },

            methods:{

                send(){

                    event.$emit("a-data",this.msg1)

                }

            }

        };

        const B={

            template:`

            <div>

                <span>我是B组件---------></span>{{msg2}}

                <input type="button" value="把b组件的数据传给c" @click="send()">

            </div>

            `,

            data(){

                return{

                    msg2:"我是B组件的数据"

                }

            },

            methods:{

                send(){

                    event.$emit("b-data",this.msg2)

                }

            }

        };

        const C={

            template:`

            <div>

                <h3>我是C组件</h3>

                <span>接收到A的数据--->{{a}}</span><br/>

                <span>接收到B的数据--->{{b}}</span>

            </div>

            `,

            data(){

              return{

                  a:"a",

                  b:"b"

              }

            },

            mounted(){

                event.$on("a-data",function (a) {

                    this.a=a;

                }.bind(this));

                event.$on("b-data",function (b) {

                    this.b=b

                }.bind(this))

            }

 

        };

        new Vue({

            el:"#div",

            data:{

                    msg:"我是父组件数据"

            },

            components:{

               "com-a":A,

               "com-b":B,

               "com-c":C

            }

        })

    }

</script>

  以上就我所了解的vue2。

Vue2与Vue3的区别主要体现在以下几个方面: 1. TypeScript支持:Vue3相对于Vue2有更好的TypeScript支持。Vue2的Options API是一个简单的对象,而TypeScript是一种类型系统,面向对象的语法,两者不太匹配。因此,Vue2需要借助vue-class-component来增强原生组件,并且需要vue-property-decorator来增加更多与Vue特性结合的装饰器,写法相对繁琐。而Vue3由TypeScript重写,对TypeScript的支持更加友好。 2. Composition API:Vue3引入了Composition API,与Vue2的Options API相比,Composition API提供了更灵活、更可组合的方式来组织和重用代码。Composition API将组件的逻辑按照功能进行组织,而不是按照选项进行组织,使得代码更加清晰、易于维护。 3. 虚拟DOM优化:Vue3在虚拟DOM上增加了patchFlag字段,用于标记组件的更新类型,从而提高渲染性能。通过patchFlag的标记,Vue3可以更精确地知道哪些组件需要更新,避免不必要的DOM操作,提高了性能。 下面是一个示例,展示了Vue2和Vue3的区别: ```html <!-- Vue2 --> <template> <div> <h1>{{ title }}</h1> <p>{{ message }}</p> </div> </template> <script> export default { data() { return { title: 'Vue2', message: 'Hello Vue2!' } } } </script> <!-- Vue3 --> <template> <div> <h1>{{ title }}</h1> <p>{{ message }}</p> </div> </template> <script> import { reactive } from 'vue' export default { setup() { const data = reactive({ title: 'Vue3', message: 'Hello Vue3!' }) return { ...data } } } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1024小神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值