Vue3新特性

一、demo:

https://github.com/Pizhong/vue3-new-featureopen in new window

or

https://gitee.com/pizhong/vue3-new-featureopen in new window

二、v-for中的Ref数组

1、官方地址:https://v3.cn.vuejs.org/guide/migration/array-refs.html#%E8%BF%81%E7%A7%BB%E7%AD%96%E7%95%A5open in new window

2、例子演示:/ref

3、总结:

vue2中,我们可以直接通过this.$refs.$property 去直接获取到数组,

vue3中,我们需要定义一个数组,并且通过一个ref绑定的函数去获取数组

三、$children

1、官方地址:https://v3.cn.vuejs.org/guide/migration/children.html#%E6%A6%82%E8%A7%88open in new window

2、例子演示:

3、总结:

在vue2中,我们可以在父组件可以通过this.$children获取到子组件的实例和属性在vue3中,$children这个属性已经被废除,使用this.$refs.$child去获取到子组件的实例以及属性

四、slot插槽

1、例子演示

2、总结:/slot

在vue2中,支持slot="name",slot-scope="scope"这样的写法

在vue3中,不支持上述写法,需改为v-solt:name = "scope"或者#name="scope"的写法

五、Composition API

1、官方地址:https://v3.cn.vuejs.org/guide/composition-api-introduction.htmlopen in new window

2、例子:/compositionApi

3、总结:

(1)所有的数据以及方法都写在setup(){}里面

(2)setup里面避免使用this。因为执行 setup 时,组件实例尚未被创建(在 setup() 内部,

this 不会是该活跃实例的引用,即不指向vue实例,Vue 为了避免我们错误的使用,直接将

setup函数中的this修改成了 undefined

(3)vue3仍然支持vue2中Options API的写法,但更推进Composition API的写法

原因:vue2中Options API 和 vue3中Composition API 的对比open in new window

六、setup

1、官方地址:https://v3.cn.vuejs.org/guide/composition-api-setup.html#%E5%8F%82%E6%95%B0open in new window

2、例子:/setUp

3、总结:

(1)setup的生命周期在beforeCreate之前,因此,组件实例尚未被创建,所以避免

使用this

(2)setup接收两个参数,propscontext

props:和vue2中组件中props意思一样,都是接收传过来的参数必须对props传过来的参数先进行声明,如果没声明,传过来的参数将出现在attrs

contextattrsemitslots

attrs :在此部分,接收在父组件传递过来的,并且没有在props中声明的参数。

emit:子组件对父组件发送事件在vue2中,子对父发送事件采用this.$emit对父组件发送事件,在vue3中子组件对父组件发送事件采用context.emit发送事件

slots:和vue2中的插槽使用类似

七、setup中的生命周期

1、官方地址:https://v3.cn.vuejs.org/guide/composition-api-lifecycle-hooks.htmlopen in new window

2、例子:/lifeCycle

八、computed、watch

1、官方地址:https://v3.cn.vuejs.org/api/computed-watch-api.html#computedopen in new window

2、例子:/computed、/watch

3、总结:

(1)当Computed只有一个回调函数的话,那么该回调函数默认就是Computed中的

Getter

(2)当Computed的参数为一个对象时,对象中就可以指定settergetter函数了

(3)watch监听reative响应式对象时,需要加上深度监听的属性,需要 deep 选项设置为 true。

(4)immediate属性为true时,页面刷新会监听数据,false时只有数据发生变化才会监听

九、拓展:

1、关于vue2和vue3响应式的区别

vue2与vue3的响应式原理open in new window

vue2的响应式原理学“废”了吗?继续观摩vue3响应式原理Proxyopen in new window

2、已经有vue2基础的,想快速上手vue3可以阅读下官方提供的vue3迁移指南

https://v3.cn.vuejs.org/guide/migration/introduction.htmlopen in new window

3、https://cn.vitejs.dev/open in new window

4、Vue 3.0 到底好在哪里?(尤雨溪点赞文)open in new window

5、Vue3的优势open in new window

6、整理的一些 Vue3 知识点open in new window

十、思考:

在什么情况下会使用vue3开发新项目?