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

Vue3中setup方法的用法詳解

 更新時間:2022年07月29日 08:38:21   作者:Kyle  
在vue3版本中,引入了一個新的函數,叫做setup。這篇文章將為大家詳細介紹一下Vue3中setup方法的用法,感興趣小伙伴的可以了解一下

1.參數props

props是一個對象,包含父組件傳遞給子組件的所有數據。在子組件中使用props進行接收。包含配置聲明并傳入的所有的屬性的對象。

也就是說,如果你想通過props的方式輸出父組件傳遞給子組件的值。你需要使用props進行接收配置。即props:{......}。如果你未通過Props進行接受配置,則輸出的值是undefined

<template>
  <div class="box">
    父組件 
  </div>
  <no-cont :mytitle="msg" 
      othertitle="別人的標題"
      @sonclick="sonclick">
  </no-cont>
</template>

<script lang="ts">
import NoCont from "../components/NoCont.vue"
export default {
  setup () {
    let msg={
      title:'父組件給子給子組件的數據'
    }
    function sonclick(msss:string){
      console.log(msss)
    }
    return {msg,sonclick}
  },
  components:{
    NoCont
  }
}
</script>

為什么通過props.mytitle輸出的值是undefined呢?

因為我們沒有使用props進行接收配置。即

props:{
    mytitle:{
        type:Object
    }
}

2.參數context

第2個參數:context,是一個對象。里面有attrs(獲取當前標簽上的所有屬性的對象)。但是該屬性是props中沒有聲明接收的所有的對象。如果你使用props去獲取值,同時props中你聲明了你要獲取的值,則:獲取的值是undefined

注意點:

attrs獲取值是不需要props中沒有聲明接收。第1個參數props獲取值是需要props中聲明接收的。有emit事件分發(fā)(傳遞給父組件需要使用該事件)

<template>
    <div @click="sonHander">
        我是子組件中的數據
    </div>
</template>

<script lang="ts">
import { defineComponent,setup } from 'vue';
export default defineComponent({
  name: 'NoCont',
  props:{
      mytitle:{
          type:Object
      }
  },
  setup(props,context){
    //輸出{title:父組件傳遞的值}
    console.log('props==>',props.mytitle);
    
    // 輸出別人的標題【使用context獲取值,不需要使用props去接受】
    console.log('context==> ',context.attrs.othertitle);
    
    // 輸出undefined,因為context不需要使用props去接受。
    console.log('contextmytitle==> ',context.attrs.mytitle);
    function sonHander(){
        context.emit('sonclick','子組件傳遞給父組件')
    }
    return {sonHander}
  }
});
</script>

3. 子組件向父組件派發(fā)事件

<template>
    <div @click="sonHander">
        我是子組件中的數據
    </div>
</template>

<script lang="ts">
import { defineComponent,setup } from 'vue';
export default defineComponent({
  name: 'NoCont',
  props:{
      mytitle:{
          type:Object
      }
  },
  setup(props,context){
    function sonHander(){
        context.emit('sonclick','子組件傳遞給父組件')
    }
    return {sonHander}
  }
});
</script>

4.優(yōu)化事件派發(fā)

我們知道第2個參數context是一個對象,并且對象中有三個屬性attrs,slots,emit,在事件派發(fā)的時候,直接使用emit就ok了

<template>
    <div @click="sonHander">
        我是子組件中的數據
    </div>
</template>

<script lang="ts">
import { defineComponent,setup } from 'vue';
export default defineComponent({
  name: 'NoCont',
  props:{
      mytitle:{
          type:Object
      }
  },
  setup(props,{attrs,slots,emit}){
    //直接使用emit進行事件派發(fā)
    function sonHander(){
        emit('sonclick','子組件傳遞給父組件')
    }
    return {sonHander}
  }
});
</script>

5.獲取父組件傳遞的值

我們將使用props參數獲取值,以及使用attrs獲取值

<template>
<hr/>
   <h2>子組件</h2>
    <div @click="sonHander">
        我是子組件中的數據
    </div>
    <h2>使用了props聲明接收==>{{ mytitle  }}</h2> 
    <h2>使用參數attrs獲取==>{{ attrs.othertitle  }}</h2> 
</template>

<script lang="ts">
import { defineComponent,setup } from 'vue';
export default defineComponent({
  name: 'NoCont',
  props:{
      mytitle:{
          type:Object
      }
  },
  setup(props,{attrs,slots,emit}){
    function sonHander(){
        emit('sonclick','子組件傳遞給父組件')
    }
    return {sonHander,attrs}
  }
});
</script>

附使用setup函數時需要注意幾點:

setup函數的執(zhí)行時機是在beforeCreatecreated之間

由于setup執(zhí)行時機是在created之間,所以組件才剛剛被創(chuàng)建,而datamethods還沒初始化好,所以無法在setup中使用datamethods

setup中this指向undefined

setup只能是同步的,不能是異步的

到此這篇關于Vue3中setup方法的用法詳解的文章就介紹到這了,更多相關Vue3 setup方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

泗水县| 巴中市| 陇西县| 东阳市| 赫章县| 井研县| 策勒县| 聂拉木县| 南皮县| 邮箱| 司法| 长武县| 翁源县| 泗水县| 江源县| 三江| 麻江县| 全州县| 昌吉市| 沾益县| 湘潭市| 兴国县| 漾濞| 平度市| 商南县| 沭阳县| 雷波县| 阳泉市| 资源县| 阿克苏市| 枞阳县| 三台县| 车致| 商河县| 资阳市| 东丰县| 揭西县| 阿城市| 洛川县| 宁城县| 金坛市|