Vue3 - setup

193 阅读1分钟
两个参数

1.props
2.context

访问组件属性,没有this

setup被执行时,该组件实例尚未建立,访问不了:

  • data
  • computed
  • methods
import {ref} from 'vue'
export default {
  name:'app',
  data(){
    return {
      name:'1'
    }
  },
  setup(){
    const name =ref('2');
    return { //必须返回 模板中才能使用
      name
    }
  }
}

setup函数中定义的变量和方法最后都是需要return出去的,不然无法在模板中使用