vue3相较于vue2的一些改变

简介: vue3相较于vue2的一些改变

vue3新增属性

setup()


setup() 函数,是 vue3 中,专门为组件提供的新属性。它为我们使用 vue3 的 Composition API 新特性提供了统一的入口。


setup 函数会在 beforeCreate 之后、created 之前执行


setup是可以完全替代beforeCreate,created的


setup函数有两个形参


第一个形参是props,就是接收父组件传递来的数据,props是一个对象,传递来的数据都在这个对象里


第二个形参是context(相当于this)。应为在setup中无法使用this,所以需要这个context来代替下this,来执行this的方法。


ref()函数


ref() 函数用来根据给定的值创建一个响应式的数据对象,ref() 函数调用的返回值是一个对象,这个对象上只包含一个 .value 属性


此方法和reactive的区别是,你在setup方法中使用ref定义的响应式数据需要.value一下才能拿到,return返回出去的时候则不需要。


reactive()函数


reactive() 函数接收一个普通对象,返回一个响应式的数据对象。、


setup() {
    // 数据
    let person = reactive({
      name:'刘泽权',
      sex:'男',
      age:20,
      job:{
        type:'前端工程师',
        salary:'30k'
      },
      hobby:["篮球","cfm"]
    })


待开发


vue3的生命周期


1、beforeCreate -> 使用 setup()


2、created -> 使用 setup()


3、beforeMount -> onBeforeMount


4、mounted -> onMounted


5、beforeUpdate -> onBeforeUpdate


6、updated -> onUpdated


7、beforeDestroy -> onBeforeUnmount


8、destroyed -> onUnmounted


9、errorCaptured -> onErrorCaptured


相关文章
|
1天前
|
JavaScript 前端开发 CDN
vue3速览
vue3速览
10 0
|
1天前
|
设计模式 JavaScript 前端开发
Vue3报错Property “xxx“ was accessed during render but is not defined on instance
Vue3报错Property “xxx“ was accessed during render but is not defined on instance
|
1天前
|
缓存 JavaScript 前端开发
Vue3 官方文档速通(中)
Vue3 官方文档速通(中)
7 0
|
1天前
|
缓存 JavaScript 前端开发
Vue3 官方文档速通(上)
Vue3 官方文档速通(上)
5 0
|
1天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之五:Pinia 状态管理
Vue3+Vite+Pinia+Naive后台管理系统搭建之五:Pinia 状态管理
6 1
|
1天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之三:vue-router 的安装和使用
Vue3+Vite+Pinia+Naive后台管理系统搭建之三:vue-router 的安装和使用
6 0
|
1天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之二:scss 的安装和使用
Vue3+Vite+Pinia+Naive后台管理系统搭建之二:scss 的安装和使用
5 0
|
1天前
|
JavaScript 前端开发 API
Vue3 系列:从0开始学习vue3.0
Vue3 系列:从0开始学习vue3.0
8 1
|
1天前
|
网络架构
Vue3 系列:vue-router
Vue3 系列:vue-router
6 2
|
1天前
Vue3+Vite+Pinia+Naive后台管理系统搭建之一:基础项目构建
Vue3+Vite+Pinia+Naive后台管理系统搭建之一:基础项目构建
6 1