国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:20日期:2022-06-10 11:34:07
目錄
  • 1、如何使用setup語法糖
  • 2、data數據的使用
  • 3、method方法的使用
  • 4、watchEffect的使用
  • 5、watch的使用
  • 6、computed計算屬性的使用
  • 7 、props父子傳值的使用
  • 8 、emit子父傳值的使用
  • 9、獲取子組件ref變量和defineExpose暴露
  • 10、路由useRoute和us eRouter的使用
  • 11、store倉庫的使用
  • 12、await的支持
  • 13、provide 和 inject 祖孫傳值

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

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

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

1、如何使用setup語法糖

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

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

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

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

4、watchEffect的使用

代碼如下(示例):

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

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

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

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("小叮當")
</script>

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

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

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)
  // 路由跳轉
  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")
  // 分發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 = "小叮當"
    }
  })
</script>
子組件代碼如下(示例):
<script setup>
  import { inject } from "vue"
  const provideState = inject("provideState")
 
  provideState.changeName()
</script>

以上就是Vue3.2中setup語法糖的使用教程分享的詳細內容,更多關于Vue setup語法糖的資料請關注其它相關文章!

標簽: JavaScript
主站蜘蛛池模板: 久久公开视频 | 国产高清天干天天视频 | 国产成人久久精品一区二区三区 | 日韩中文字幕视频 | 精品久久中文字幕有码 | 久久视屏这里只有精品6国产 | 国产在线综合视频 | 亚洲欧美在线综合一区二区三区 | 高清不卡毛片免费观看 | 激情宗合 | 手机看片精品国产福利盒子 | 国产三级a三级三级三级 | 久久成人在线 | 在线播放精品一区二区啪视频 | 国产欧美视频综合二区 | 欧美黑粗特黄午夜大片 | 一级免费a | 在线免费观看成年人视频 | 日本在线www| 国产成人www免费人成看片 | 欧美在线高清 | 精品欧美日韩一区二区 | 亚洲高清视频在线播放 | 欧美特级大片 | 免费欧美一级 | 久久国产精品久久久久久久久久 | 国产日韩欧美在线观看播放 | 欧美人成在线观看ccc36 | 狠色狠狠色狠狠狠色综合久久 | 未满14周岁啪啪网站 | 国产一二三区视频 | 欧美一级第一免费高清 | 日本黄色免费大片 | 亚洲毛片免费视频 | 天天碰夜夜操 | 永久免费看毛片 | 欧美一级片毛片 | 免费高清一级欧美片在线观看 | 国产精品免费一区二区三区 | 亚洲国产天堂久久精品网 | 欧美国产合集在线视频 |