Fork me on GitHub

Vue3的setup()学习

setup()执行的时机

  • setup()在beforeCreate之前执行一次,this是undefined
    image
    image

setup()的参数

1.props

props值为对象,包含:组件外部传过来的且组件内部声明接收了的属性。
image
查看一下:
image

2.context

上下文对象,包含以下三个内容:

attrs:值为对象。包含组件外部传过来且在组件内部没有声明的属性,相当于this.$attrs.

以下sex属性没有在Demo.vue组件中声明。
image
可以看到:
image

slots:收到的插槽内容。

emit: 分发自定义事件的函数,相当于this.$emit

和VUE2有区别的地方在于,子组件发送事件时

  • 使用context.emit('xxx', 'xxx')
  • 要和props一样,写一个emits:[""],否则报警告。
    image
posted @ 2021-07-21 20:29  zerozhupan  阅读(541)  评论(0)    收藏  举报