setup语法糖

蔚落 2024-04-01 12:37 181阅读 0赞

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

1、如何使用setup语法糖

只需在 script 标签上写上 setup

代码如下(示例):

  1. <template>
  2. </template>
  3. <script setup>
  4. </script>
  5. <style scoped>
  6. </style>

2、data数据的使用

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

代码如下(示例):

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

3、method方法的使用

代码如下(示例):

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

4、watchEffect的使用

代码如下(示例):

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

5、watch的使用

代码如下(示例):

  1. <script setup>
  2. import {
  3. reactive,
  4. watch,
  5. } from 'vue'
  6. //数据
  7. let sum = ref(0)
  8. let msg = ref('你好啊')
  9. let person = reactive({
  10. name:'张三',
  11. age:18,
  12. job:{
  13. j1:{
  14. salary:20
  15. }
  16. }
  17. })
  18. // 两种监听格式
  19. watch([sum,msg],(newValue,oldValue)=>{
  20. console.log('sum或msg变了',newValue,oldValue)
  21. },{
  22. immediate:true})
  23. watch(()=>person.job,(newValue,oldValue)=>{
  24. console.log('person的job变化了',newValue,oldValue)
  25. },{
  26. deep:true})
  27. </script>

6、computed计算属性的使用

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

代码如下(示例):

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

7 、props父子传值的使用

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

8 、emit子父传值的使用

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

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

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

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

10、路由useRoute和us eRouter的使用

代码如下(示例):

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

11.await的支持

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

代码如下(示例):

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

发表评论

表情:
评论列表 (有 0 条评论,181人围观)

还没有评论,来说两句吧...

相关阅读

    相关 setup语法

    > 提示: vue3.2 版本开始才能使用语法糖! 1、如何使用setup语法糖 > 只需在 script 标签上写上 setup 代码如下(示例): <t

    相关 语法2

    语法糖,意指那些没有给计算机语言添加新功能,而只是对人类来说更“sweet”的语法,意在使得编程风格更易读。C\2.0,3.0发布的新特性,除了泛型不是语法糖,其他所有的新特性

    相关 语法 语法

            语法糖(Syntactic sugar),也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添

    相关 Java语法

    > 语法糖(Syntactic Sugar),也称糖衣语法,指在计算机语言中添加的某种语法,这种语法对语言本身功能来说没有什么影响,只是为了方便程序员的开发,提高开发效率。说白

    相关 java语法

    [java语法糖][java] 语法糖(Syntactic Sugar),也称糖衣语法,指在计算机语言中添加的某种语法,这种语法对语言本身功能来说没有什么影响,只是为了方