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