最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文搞懂Vue3.2中setup語法糖使用

 更新時(shí)間:2022年07月06日 10:49:03   作者:玲小叮當(dāng)  
在vue3版本中,引入了一個(gè)新的函數(shù),叫做setup。本文將通過實(shí)例為大家詳細(xì)講講Vue3.2中setup語法糖的使用,感興趣的小伙伴可以了解一下

前言

提示:Vue3.2 版本開始才能使用語法糖!在 Vue3.0 中變量必須 return 出來,template中才能使用;而在 Vue3.2 中只需要在 script 標(biāo)簽上加上 setup 屬性,無需 returntemplate 便可直接使用,非常的香啊!

提示:以下是本篇文章正文內(nèi)容,下面案例可供參考

一、如何使用setup語法糖

只需在 script 標(biāo)簽上寫上setup

代碼如下(示例):

<template>
</template>
<script setup>
</script>
<style scoped lang="less">
</style>

二、data數(shù)據(jù)的使用

由于 setup 不需寫 return,所以直接聲明數(shù)據(jù)即可

代碼如下(示例):

<script setup>
    import {
      ref,
      reactive,
      toRefs,
    } from 'vue'
    
    const data = reactive({
      patternVisible: false,
      debugVisible: false,
      aboutExeVisible: false,
    })
    
    const content = ref('content')
    //使用toRefs解構(gòu)
    const { patternVisible, debugVisible, aboutExeVisible } = toRefs(data)
</script>

三、method方法的使用

代碼如下(示例):

<template >
    <button @click="onClickHelp">系統(tǒng)幫助</button>
</template>
<script setup>
import {reactive} from 'vue'

const data = reactive({
      aboutExeVisible: false,
})
// 點(diǎn)擊幫助
const onClickHelp = () => {
    console.log(`系統(tǒng)幫助`)
    data.aboutExeVisible = true
}
</script>

四、watchEffect的使用

代碼如下(示例):

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

let sum = ref(0)

watchEffect(()=>{
  const x1 = sum.value
  console.log('watchEffect所指定的回調(diào)執(zhí)行了')
})
</script>

五、watch的使用

代碼如下(示例):

<script setup>
    import {
      reactive,
      watch,
    } from 'vue'
     //數(shù)據(jù)
     let sum = ref(0)
     let msg = ref('你好啊')
     let person = reactive({
                    name:'張三',
                    age:18,
                    job:{
                      j1:{
                        salary:20
                      }
                    }
                  })
    // 兩種監(jiān)聽格式
    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>

六、computed計(jì)算屬性的使用

computed計(jì)算屬性有兩種寫法(簡寫和考慮讀寫的完整寫法)

代碼如下(示例):

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

    //數(shù)據(jù)
    let person = reactive({
       firstName:'小',
       lastName:'叮當(dāng)'
     })
    // 計(jì)算屬性簡寫
    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>

七、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('小叮當(dāng)')
</script>

八、emit子父傳值的使用

子組件代碼如下(示例):

<template>
   <a-button @click="isOk">
     確定
   </a-button>
</template>
<script setup>
import { defineEmits } from 'vue';

// emit
const emit = defineEmits(['aboutExeVisible'])
/**
 * 方法
 */
// 點(diǎn)擊確定按鈕
const isOk = () => {
  emit('aboutExeVisible');
}
</script>

父組件代碼如下(示例):

<template>
  <AdoutExe @aboutExeVisible="aboutExeHandleCancel" />
</template>
<script setup>
import {reactive} from 'vue'
// 導(dǎo)入子組件
import AdoutExe from '../components/AdoutExeCom'

const data = reactive({
  aboutExeVisible: false, 
})
// content組件ref


// 關(guān)于系統(tǒng)隱藏
const aboutExeHandleCancel = () => {
  data.aboutExeVisible = false
}

</script>

九、獲取子組件ref變量和defineExpose暴露

即vue2中的獲取子組件的ref,直接在父組件中控制子組件方法和變量的方法

子組件代碼如下(示例):

<template>
    <p>{{data }}</p>
</template>

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

/**
 * 數(shù)據(jù)部分
 * */
const data = reactive({
  modelVisible: false,
  historyVisible: false, 
  reportVisible: false, 
})
defineExpose({
  ...toRefs(data),
})
</script>

父組件代碼如下(示例):

<template>
    <button @click="onClickSetUp">點(diǎn)擊</button>
    <Content ref="content" />
</template>

<script setup>
import {ref} from 'vue'

// content組件ref
const content = ref('content')
// 點(diǎn)擊設(shè)置
const onClickSetUp = ({ key }) => {
   content.value.modelVisible = true
}

</script>
<style scoped lang="less">
</style>

十、路由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)

  // 路由跳轉(zhuǎn)
  router.push({
      path: `/index`
  })
</script>

十一、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')
  
  // 分發(fā)actions的方法
  store.dispatch('fnName')
</script>

十二、await 的支持

setup 語法糖中可直接使用 await,不需要寫 async , setup 會(huì)自動(dòng)變成 async setup

代碼如下(示例):

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

十三、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 = '小叮當(dāng)'
    }
  })
</script>

子組件代碼如下(示例):

<script setup>
  import { inject } from 'vue'
  const provideState = inject('provideState')

  provideState.changeName()
</script>

以上就是一文搞懂Vue3.2中setup語法糖使用的詳細(xì)內(nèi)容,更多關(guān)于Vue setup語法糖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue自定義實(shí)例化modal彈窗功能的實(shí)現(xiàn)

    vue自定義實(shí)例化modal彈窗功能的實(shí)現(xiàn)

    這篇文章主要介紹了vue自定義實(shí)例化modal彈窗,Vue.extend 屬于Vue的全局 api,在實(shí)際業(yè)務(wù)開發(fā)中我們很少使用,因?yàn)橄啾瘸S玫?nbsp;Vue.component寫法使用 extend 步驟要更加繁瑣一些,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下
    2022-09-09
  • 使用Vue純前端實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼并實(shí)現(xiàn)倒計(jì)時(shí)

    使用Vue純前端實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼并實(shí)現(xiàn)倒計(jì)時(shí)

    在實(shí)際的應(yīng)用開發(fā)中,涉及用戶登錄驗(yàn)證、密碼重置等場景時(shí),通常需要前端實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼的功能,以提升用戶體驗(yàn)和安全性,以下是一個(gè)簡單的前端實(shí)現(xiàn),演示了如何在用戶點(diǎn)擊發(fā)送驗(yàn)證碼按鈕時(shí)觸發(fā)短信驗(yàn)證碼的發(fā)送,并開始一個(gè)倒計(jì)時(shí)
    2024-04-04
  • vue3版本網(wǎng)頁小游戲設(shè)計(jì)思路

    vue3版本網(wǎng)頁小游戲設(shè)計(jì)思路

    最近火爆全網(wǎng)的羊了個(gè)羊小程序,背景是根據(jù)官方介紹,“羊了個(gè)羊”是一款闖關(guān)消除小游戲,通關(guān)率不到0.1%。主要玩法為重疊的各類方塊,需要在下方7個(gè)欄內(nèi)完成消除,其特點(diǎn)就是“極難”,也因此成為熱門挑戰(zhàn),對(duì)vue3版本網(wǎng)頁小游戲設(shè)計(jì)思路感興趣的朋友跟隨小編一起看看吧
    2022-12-12
  • vue中v-for循環(huán)選中點(diǎn)擊的元素并對(duì)該元素添加樣式操作

    vue中v-for循環(huán)選中點(diǎn)擊的元素并對(duì)該元素添加樣式操作

    這篇文章主要介紹了vue中v-for循環(huán)選中點(diǎn)擊的元素并對(duì)該元素添加樣式操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue+webpack實(shí)現(xiàn)異步組件加載的方法

    vue+webpack實(shí)現(xiàn)異步組件加載的方法

    下面小編就為大家分享一篇vue+webpack實(shí)現(xiàn)異步組件加載的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • 解析Vue2 dist 目錄下各個(gè)文件的區(qū)別

    解析Vue2 dist 目錄下各個(gè)文件的區(qū)別

    本篇文章主要介紹了解析Vue2 dist 目錄下各個(gè)文件的區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • vue音樂播放器插件vue-aplayer的配置及其使用實(shí)例詳解

    vue音樂播放器插件vue-aplayer的配置及其使用實(shí)例詳解

    本篇文章主要介紹了vue音樂播放器插件vue-aplayer的配置及其使用實(shí)例詳解,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-07-07
  • Vue 請(qǐng)求傳公共參數(shù)的操作

    Vue 請(qǐng)求傳公共參數(shù)的操作

    這篇文章主要介紹了Vue 請(qǐng)求傳公共參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • element使用自定義icon圖標(biāo)的詳細(xì)步驟

    element使用自定義icon圖標(biāo)的詳細(xì)步驟

    前端經(jīng)常會(huì)用到UI提供的各種圖表,推薦阿里的圖標(biāo)庫,下面這篇文章主要給大家介紹了關(guān)于element使用自定義icon圖標(biāo)的詳細(xì)步驟,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • Vue中的event對(duì)象介紹

    Vue中的event對(duì)象介紹

    這篇文章介紹了Vue中的event對(duì)象,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03

最新評(píng)論

安阳市| 和田市| 嘉义市| 贵溪市| 嘉荫县| 桐梓县| 香港 | 含山县| 上林县| 石城县| 环江| 乌海市| 苗栗县| 汝南县| 保德县| 全南县| 莎车县| 施秉县| 涟水县| 盐源县| 张家港市| 措美县| 盐边县| 抚顺县| 香格里拉县| 连州市| 托克逊县| 汉中市| 永兴县| 关岭| 米脂县| 安徽省| 古田县| 阿合奇县| 偏关县| 西乡县| 江源县| 陵水| 防城港市| 岑巩县| 焦作市|