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

Vue3.2中setup語法糖的使用教程分享

 更新時間:2023年05月16日 11:19:48   作者:Yxj-5211314  
這篇文章主要為大家詳細(xì)介紹了Vue3.2中setup語法糖的具體使用方法,文中的示例代碼講解詳細(xì),對我們深入了解Vue有一定的幫助,需要的可以參考一下

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

在 Vue3.0 中變量必須  return  出來, template 中才能使用;而在 Vue3.2 中只需要在  script  標(biāo)簽上加上  setup  屬性,無需  return , template  便可直接使用,非常的香?。?/p>

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

1、如何使用setup語法糖

只需在  script  標(biāo)簽上寫上 setup 代碼如下(示例):

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

2、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>

3、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>

4、watchEffect的使用

代碼如下(示例):

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

5、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>

6、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>

7 、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>

8 、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>

9、獲取子組件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>

10、路由useRoute和us eRouter的使用

代碼如下(示例):

<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>

11、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>

12、await的支持

setup  語法糖中可直接使用  await ,不需要寫  async  ,  setup  會自動變成  async setup

代碼如下(示例):

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

13、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語法糖的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 前端vue中文件下載的三種方式匯總

    前端vue中文件下載的三種方式匯總

    對于Vue中實(shí)現(xiàn)一般的下載功能很簡單,下面這篇文章主要給大家介紹了關(guān)于前端vue中文件下載的三種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • Vue組件的通信方式詳解

    Vue組件的通信方式詳解

    這篇文章主要介紹的是Vue組件間的通信方式,本文將系統(tǒng)的介紹了幾種不使用Vuex,比較實(shí)用的組件間的通信方式,希望能幫助到大家
    2023-04-04
  • 8個非常實(shí)用的Vue自定義指令

    8個非常實(shí)用的Vue自定義指令

    這篇文章主要介紹了8個非常實(shí)用的Vue自定義指令,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-12-12
  • vue腳手架及vue-router基本使用

    vue腳手架及vue-router基本使用

    這篇文章主要介紹了vue腳手架及vue-router基本使用,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-04-04
  • vue3前端axios請求報(bào)錯network錯誤的解決辦法

    vue3前端axios請求報(bào)錯network錯誤的解決辦法

    在使用Axios進(jìn)行網(wǎng)絡(luò)請求時,遇到“Network?Error”報(bào)錯可能是由多種原因?qū)е碌?這篇文章主要介紹了vue3前端axios請求報(bào)錯network錯誤的解決辦法,需要的朋友可以參考下
    2025-08-08
  • node.js開發(fā)輔助工具nodemon安裝與配置詳解

    node.js開發(fā)輔助工具nodemon安裝與配置詳解

    node.js代碼修改后,需要重新啟動 Express 應(yīng)用,所做的修改才能生效。若之后的每次代碼修改都要重復(fù)這樣的操作,勢必會影響開發(fā)效率,本文將詳細(xì)介紹Nodemon,它會監(jiān)測項(xiàng)目中的所有文件,一旦發(fā)現(xiàn)文件有改動,Nodemon 會自動重啟應(yīng)用
    2020-02-02
  • vue3+element?plus中利用el-menu如何實(shí)現(xiàn)路由跳轉(zhuǎn)

    vue3+element?plus中利用el-menu如何實(shí)現(xiàn)路由跳轉(zhuǎn)

    這篇文章主要給大家介紹了關(guān)于vue3+element?plus中利用el-menu如何實(shí)現(xiàn)路由跳轉(zhuǎn)的相關(guān)資料,在Vue?Router中我們可以使用el-menu組件來實(shí)現(xiàn)菜單導(dǎo)航,通過點(diǎn)擊菜單項(xiàng)來跳轉(zhuǎn)到不同的路由頁面,需要的朋友可以參考下
    2023-12-12
  • vue添加axios,并且指定baseurl的方法

    vue添加axios,并且指定baseurl的方法

    今天小編就為大家分享一篇vue添加axios,并且指定baseurl的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vue3 elementPlus 表格實(shí)現(xiàn)行列拖拽及列檢索功能(完整代碼)

    vue3 elementPlus 表格實(shí)現(xiàn)行列拖拽及列檢索功能(完整代碼)

    本文通過實(shí)例代碼給大家介紹vue3 elementPlus 表格實(shí)現(xiàn)行列拖拽及列檢索功能,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-10-10
  • Vue3請求后端接口實(shí)現(xiàn)方式

    Vue3請求后端接口實(shí)現(xiàn)方式

    本文詳解Vue3中使用Fetch和Axios與后端交互的方法,涵蓋請求發(fā)起、狀態(tài)管理、錯誤處理及跨域等解決方案,推薦優(yōu)先使用Axios,其攔截器、自動轉(zhuǎn)換等特性提升開發(fā)效率,建議封裝實(shí)例與API函數(shù)以統(tǒng)一配置
    2025-09-09

最新評論

出国| 霸州市| 廊坊市| 南溪县| 探索| 永靖县| 乐陵市| 东光县| 鹤壁市| 河间市| 宝应县| 丰顺县| 田阳县| 江达县| 汶川县| 松潘县| 贺州市| 喀什市| 罗江县| 忻城县| 绩溪县| 昌宁县| 徐州市| 民和| 宽城| 蚌埠市| 宜州市| 巴林左旗| 蕉岭县| 东乌珠穆沁旗| 韩城市| 衡山县| 安龙县| 车险| 太湖县| 皮山县| 武川县| 定陶县| 祁连县| 碌曲县| 前郭尔|