Vue3setup()的非语法糖和语法糖的用法

1、setup()的语法糖的用法

script标签上写setup属性,不需要export default {} setup() 都可以省

创建每个属性或方法时也不需要return

导入某个组件时也不需要注册

<script setup >
// script标签上写setup属性,不需要export  default  {} setup() 都可以省
import {ref} from 'vue'

    //组合式api
    //创建响应式数据age,初始值是10
    const age=ref(10)
    const sname=ref('zs')
    //修改年龄的方法

    const increase=()=>{
      age.value++
    }
    //模板需要用的数据或方法,需要 return


</script>

<template>
<div>hello,world</div>
<p>年龄{
  { age }}</p>
<p>姓名:{
  { sname }}</p>
<button @click="increase">年龄+1</button>
</template>

<style scoped>

</style>

2、setup()的非语法糖的用法

创建多个数据就要return多个数据

setup() 组合式api的入口,执行的时机比beforCreate还早

<script >
import {ref} from 'vue'
export default {
  setup(){
    //组合式api
    //创建响应式数据age,初始值是10
    const age=ref(10)
    const sname=ref('zs')
    //修改年龄的方法

    const increase=()=>{
      age.value++
    }
    //模板需要用的数据或方法,需要 return

    return {
      age,
      increase,
      sname
    }
  }
}
</script>

<template>
<div>hello,world</div>
<p>年龄{
  { age }}</p>
<p>姓名:{
  { sname }}</p>
<button @click="increase">年龄+1</button>
</template>

<style scoped>

</style>

相关推荐

  1. Vue3setup()语法语法

    2024-01-23 04:06:02       30 阅读
  2. vuesetup语法

    2024-01-23 04:06:02       19 阅读
  3. vue3 setup语法

    2024-01-23 04:06:02       42 阅读
  4. vuesetup语法优点

    2024-01-23 04:06:02       26 阅读
  5. Python语法

    2024-01-23 04:06:02       36 阅读
  6. vuesync语法使用

    2024-01-23 04:06:02       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-23 04:06:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-23 04:06:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-23 04:06:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-23 04:06:02       20 阅读

热门阅读

  1. Go语言实现跳动的爱心(附带源码)

    2024-01-23 04:06:02       38 阅读
  2. 面试 Vue 框架八股文十问十答第七期

    2024-01-23 04:06:02       34 阅读
  3. 如何调整服务器系统时间

    2024-01-23 04:06:02       28 阅读
  4. spring boot 常用的 Cron 表达式

    2024-01-23 04:06:02       38 阅读
  5. 决斗(线段树)

    2024-01-23 04:06:02       30 阅读
  6. Quarkus 2.8.0引入了细粒度的Transaction API

    2024-01-23 04:06:02       33 阅读
  7. MySQL索引优化:深入理解索引合并

    2024-01-23 04:06:02       32 阅读
  8. Android扫码方案

    2024-01-23 04:06:02       33 阅读
  9. Vue中的模式和环境变量

    2024-01-23 04:06:02       24 阅读
  10. 各行业领域向chatgpt高质量提问的方式

    2024-01-23 04:06:02       31 阅读
  11. docker

    2024-01-23 04:06:02       34 阅读