vue3.x教程-setup() 函数介绍

介绍

setup() 是 Vue3 组合式API 的 入口函数,数据、方法、计算属性、监视….等等,均配置在 setup() 函数中

特点

  • 选项式 setup 必须 return 一个对象,对象里的属性 / 方法才能在 <template> 模板中直接使用;

  • this 不可用:setup() 执行时组件实例未创建,this 是 undefined

  • 执行时机:beforeCreate 之前,所以拿不到 data、methods、computed。

两种写法

  1. 选项式:setup(){}
  2. 语法糖(推荐):<script setup>(编译时语法糖,更简洁)

原文出处:/show_1GW3gMqYwTUb.html