亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue3中setup-script的應用實例

 更新時間:2022年01月18日 10:40:55   作者:MWH  
script-setup是一個比較有爭議的新特性,作為 setup 函數(shù)的語法糖,褒貶不一,不過經(jīng)歷了幾次迭代之后,目前在體驗上來說,感受還是非常棒的,這篇文章主要給大家介紹了關(guān)于vue3中setup-script應用的相關(guān)資料,需要的朋友可以參考下

新特性的產(chǎn)生背景

在了解它怎么用之前,可以先了解一下它被推出的一些背景,可以幫助你對比開發(fā)體驗上的異同點,以及了解為什么會有這一章節(jié)里面的新東西。

在 Vue 3.0 的 .vue 組件里,遵循 SFC 規(guī)范要求(注:SFC,即 Single-File Component,.vue 單組件),標準的 setup 用法是,在 setup 里面定義的數(shù)據(jù)如果需要在 template 使用,都需要 return 出來。

如果你使用的是 TypeScript ,還需要借助 defineComponent 來幫助你對類型的自動推導。

<!-- 標準組件格式 -->
<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  setup () {
    // ...

    return {
      // ...
    }
  }
})
</script>

關(guān)于標準 setup 和 defineComponent 的說明和用法,可以查閱 全新的 setup 函數(shù) 一節(jié)。

script-setup 的推出是為了讓熟悉 3.0 的用戶可以更高效率的開發(fā)組件,減少一些心智負擔,只需要給 script 標簽添加一個 setup 屬性,那么整個 script 就直接會變成 setup 函數(shù),所有頂級變量、函數(shù),均會自動暴露給模板使用(無需再一個個 return 了)。

上次寫了關(guān)于自己初次使用vue3的一些感受,同時也獲取了一眾大佬的教導,其中最重要的是方法的setup的語法糖,為了搞清楚這個語法糖,我自己有把之前的頁面,又重新重構(gòu)了一次。果然得到真香定律,使用以后發(fā)現(xiàn)原來vue3還可以像react那樣簡潔的處理方法和傳值,話不多說上代碼看下吧

內(nèi)部變量

首先看下內(nèi)部變量變化,這是單純之前使用setup

    <template>
    <div>
       <div>
            子組件內(nèi)String:{{infor}}
       </div>
        <div>
            子組件內(nèi)obj:{{obj.data}}
        </div>
        <div>
            子組件內(nèi)arry:{{arry[0]}}
        </div>
        <div @click="changeInfor">
            改變obj
        </div>
    </div>
   
</template>

<script>
    import { ref, onMounted, defineEmits, defineProps, defineExpose, reactive } from "vue";
    export default {
        setup(){
             const infor = ref('infor')
             const obj  = reactive({
                data:'obj'
            })
            const arry  = reactive([111,222,333])
            const changeInfor  = () =>{
                obj.data = 'changeObj'
            }

            return {
                infor, obj, arry, changeInfor
            }
        }
    }
</script>

<style>
  div{
      line-height: 40px;
  }
</style>

這是改成語法糖之后的代碼

    <template>
    <div>
       <div>
            子組件內(nèi)String:{{infor}}
       </div>
        <div>
            子組件內(nèi)obj:{{obj.data}}
        </div>
        <div>
            子組件內(nèi)arry:{{arry[0]}}
        </div>
        <div @click="changeInfor">
            改變obj
        </div>
    </div>
   
</template>

<script setup>
      import { ref, onMounted, defineEmits, defineProps, defineExpose, reactive } from "vue";
      const infor = ref('infor')
      const obj  = reactive({
          data:'obj'
      })
      const arry  = reactive([111,222,333])
      const changeInfor  = () =>{
          infor.value = '32323'
          obj.data = 'changeObj'
      }
</script>

<style>
  div{
      line-height: 40px;
  }
</style>

這里可以明顯看出來,未使用setup-script的方式,跟傳統(tǒng)的還是有很大區(qū)別的, 結(jié)構(gòu)簡單明了,不需要把大量的變量和方法都寫在setup這個函數(shù)里面,自由度很高,有點像react的類里面的使用方法

子父級傳值

這里面主要涉及到三個方法 defineEmits,defineProps,defineExpose

// 父組件
<template>
    <div>
        父組件
        <children ref="child" @getData="sentData" :data="data"/>
        <div>{{childData || '我還沒收到值'}}</div>
        <div @click="makeClid">點擊調(diào)用子組件方法</div>
    </div>
</template>
<script setup>
  import { ref, onMounted, defineEmits, defineProps, defineExpose, reactive } from "vue";
  import children from './components/children.vue'
  const childData = ref('')
  const data = ref('父組件給的值prop傳值')
  const sentData = (data) =>{
    childData.value = data
  }
  const child = ref(null) // 獲取子組件ref
  const makeClid = () =>{
    child.value.setData('子組件已調(diào)用')
  }
</script>


// 子組件
<template>
    <div>
       {{fatherData || '父組件還未調(diào)用我'}}
       <div @click="getData">觸發(fā)父組件</div>
       <div>fatherProps:{{fatherProps}}</div>
    </div>
   
</template>

<script setup>
      import { ref, onMounted, defineEmits, defineProps, defineExpose, reactive } from "vue";
      const fatherData = ref('')
      const fatherProps = ref('')
      const props = defineProps({ // 父組件傳值
          data:String
      })
        fatherProps.value = props.data
      const emit = defineEmits(['getData']) // 接受父組件數(shù)據(jù)
      const getData = () =>{
          emit('getData','給父組件的值')  // 觸發(fā)父組件的方法
      }

      const setData = (val) =>{ // 父組件調(diào)用
          fatherData.value = val
      }

      defineExpose({  // 拋出方法
            getData,
            setData
        })
</script>
  • 子組件調(diào)用父組件:這點很好理解,跟vue2差不多的形式,父組件里面掛載@getData,子組件里面通過defineEmits這個方法獲取,最后調(diào)用方式跟之前也是一樣的
  • 父組件調(diào)用子組件:這點區(qū)別還是很大的,需要子組件先定義好方法,然后通過defineExpose暴露出去,父組件創(chuàng)建ref,這里需要創(chuàng)建的變量名字和子組件的ref名字一直,否者獲取不到,最后通過獲取拋出的value找到對應的方法。

總結(jié)

到此這篇關(guān)于vue3中setup-script應用的文章就介紹到這了,更多相關(guān)vue3 setup-script應用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論