一文掌握 vue3.2 setup 语法糖

开发 前端
在 Vue3.0 中变量必须 return 出来,template中才能使用;而在 Vue3.2 中只需要在 script 标签上加上 setup 属性,无需 return,template 便可直接使用,非常的香啊!

前言

提示:Vue3.2 版本开始才能使用语法糖!

在 Vue3.0 中变量必须 return 出来,template中才能使用;而在 Vue3.2 中只需要在 script 标签上加上 setup 属性,无需 return,template 便可直接使用,非常的香啊!

提示:以下是本篇文章正文内容,下面案例可供参考

一、如何使用setup语法糖

只需在 script 标签上写上setup

代码如下(示例):

<template>
</template>
<script setup>
</script>
<style scoped lang="less">
</style>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

二、data数据的使用

由于 setup 不需写 return,所以直接声明数据即可

代码如下(示例):

<script setup>
   import {
     ref,
     reactive,
     toRefs,
   } from 'vue'

   const data = reactive({
     patternVisible: false,
     debugVisible: false,
     aboutExeVisible: false,
   })
   const content = ref('content')
   //使用toRefs解构
   const { patternVisible, debugVisible, aboutExeVisible } = toRefs(data)
</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

三、method方法的使用

代码如下(示例):

<template >
   <button @click="onClickHelp">系统帮助</button>
</template>
<script setup>
import {reactive} from 'vue'
const data = reactive({
     aboutExeVisible: false,
})
// 点击帮助
const onClickHelp = () => {
   console.log(`系统帮助`)
   data.aboutExeVisible = true
}
</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

四、watchEffect的使用

代码如下(示例):

<script setup>
import {
 ref,
 watchEffect,
} from 'vue'
let sum = ref(0)
watchEffect(()=>{
 const x1 = sum.value
 console.log('watchEffect所指定的回调执行了')
})
</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

五、watch的使用

代码如下(示例):

<script setup>
   import {
     reactive,
     watch,
   } from 'vue'
    //数据
    let sum = ref(0)
    let msg = ref('你好啊')
    let person = reactive({
                   name:'张三',
                   age:18,
                   job:{
                     j1:{
                       salary:20
                     }
                   }
                 })
   // 两种监听格式
   watch([sum,msg],(newValue,oldValue)=>{
           console.log('sum或msg变了',newValue,oldValue)
         },{immediate:true})
    watch(()=>person.job,(newValue,oldValue)=>{
       console.log('person的job变化了',newValue,oldValue)
     },{deep:true})  

</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

六、computed计算属性的使用

computed计算属性有两种写法(简写和考虑读写的完整写法)

代码如下(示例):

<script setup>
   import {
     reactive,
     computed,
   } from 'vue'
   //数据
   let person = reactive({
      firstName:'小',
      lastName:'叮当'
    })
   // 计算属性简写
   person.fullName = computed(()=>{
       return person.firstName + '-' + person.lastName
      })  
   // 完整写法
   person.fullName = computed({
     get(){
       return person.firstName + '-' + person.lastName
     },
     set(value){
       const nameArr = value.split('-')
       person.firstName = nameArr[0]
       person.lastName = nameArr[1]
     }
   })
</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

七、props父子传值的使用

子组件代码如下(示例):

<template>
 <span>{{props.name}}</span>
</template>
<script setup>
 import { defineProps } from 'vue'
 // 声明props
 const props = defineProps({
   name: {
     type: String,
     default: '11'
   }
 })  
 // 或者
 //const props = defineProps(['name'])
</script>
父组件代码如下(示例):
<template>
 <child :name='name'/>  
</template>
<script setup>
   import {ref} from 'vue'
   // 引入子组件
   import child from './child.vue'
   let name= ref('小叮当')
</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

八、emit子父传值的使用

子组件代码如下(示例):

<template>
  <a-button @click="isOk">
    确定
  </a-button>
</template>
<script setup>
import { defineEmits } from 'vue';
// emit
const emit = defineEmits(['aboutExeVisible'])
/**
* 方法
*/
// 点击确定按钮
const isOk = () => {
 emit('aboutExeVisible');
}
</script>
父组件代码如下(示例):
<template>
 <AdoutExe @aboutExeVisible="aboutExeHandleCancel" />
</template>
<script setup>
import {reactive} from 'vue'
// 导入子组件
import AdoutExe from '../components/AdoutExeCom'
const data = reactive({
  aboutExeVisible: false,  
})
// content组件ref
// 关于系统隐藏
const aboutExeHandleCancel = () => {
 data.aboutExeVisible = false
}
</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.

九、获取子组件ref变量和defineExpose暴露

即vue2中的获取子组件的ref,直接在父组件中控制子组件方法和变量的方法

子组件代码如下(示例):

<template>
   <p>{{data }}</p>
</template>
<script setup>
import {
 reactive,
 toRefs
} from 'vue'
/**
* 数据部分
* */
const data = reactive({
 modelVisible: false,
  historyVisible: false,  
  reportVisible: false,  
})
defineExpose({
 ...toRefs(data),
})
</script>
父组件代码如下(示例):
<template>
   <button @click="onClickSetUp">点击</button>
   <Content ref="content" />
</template>
<script setup>
import {ref} from 'vue'
// content组件ref
const content = ref('content')
// 点击设置
const onClickSetUp = ({ key }) => {
  content.value.modelVisible = true
}
</script>
<style scoped lang="less">
</style>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.

十、路由useRoute和useRouter的使用

代码如下(示例):

<script setup>
 import { useRoute, useRouter } from 'vue-router'
 // 声明
 const route = useRoute()
 const router = useRouter()
 // 获取query
 console.log(route.query)
 // 获取params
 console.log(route.params)
 // 路由跳转
 router.push({
     path: `/index`
 })
</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

十一、store仓库的使用

代码如下(示例):

<script setup>
 import { useStore } from 'vuex'
 import { num } from '../store/index'
 const store = useStore(num)
 // 获取Vuex的state
 console.log(store.state.number)
 // 获取Vuex的getters
 console.log(store.state.getNumber)
 // 提交mutations
 store.commit('fnName')
 // 分发actions的方法
 store.dispatch('fnName')
</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

十二、await的支持

setup 语法糖中可直接使用 await,不需要写 async , setup 会自动变成 async setup

代码如下(示例):

<script setup>
 import Api from '../api/Api'
 const data = await Api.getData()
 console.log(data)
</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

十三、provide 和 inject 祖孙传值

父组件代码如下(示例):

<template>
 <AdoutExe />
</template>
<script setup>
 import { ref,provide } from 'vue'
 import AdoutExe from '@/components/AdoutExeCom'
 let name = ref('Jerry')
 // 使用provide
 provide('provideState', {
   name,
   changeName: () => {
     name.value = '小叮当'
   }
 })
</script>
子组件代码如下(示例):
<script setup>
 import { inject } from 'vue'
 const provideState = inject('provideState')
 provideState.changeName()
</script>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
责任编辑:庞桂玉 来源: 前端大全
相关推荐

2023-04-27 11:07:24

Setup语法糖Vue3

2024-03-15 08:45:31

Vue 3setup语法

2022-12-20 07:39:46

2023-12-21 17:11:21

Containerd管理工具命令行

2022-10-21 17:24:34

契约测试定位

2022-07-13 10:07:31

vue3组件监听器

2021-05-12 18:22:36

Linux 内存管理

2021-12-15 08:23:42

Vue3 插件Vue应用

2023-10-24 11:44:21

2022-07-06 10:02:12

Vue3.2ExposeAPI

2020-10-09 07:56:52

Linux

2023-12-15 09:45:21

阻塞接口

2022-09-02 15:22:32

Vue3.2技术代码

2017-11-28 15:20:27

Python语言编程

2021-02-22 09:05:59

Linux字符设备架构

2023-03-10 07:57:26

2023-08-01 09:27:44

Golang模糊测试

2020-12-18 11:54:22

Linux系统架构

2024-11-19 09:00:00

Pythondatetime模块

2021-06-04 09:35:05

Linux字符设备架构
点赞
收藏

51CTO技术栈公众号