关于前端: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事件散发(传递给父组件须要应用该事件)

<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. 子组件向父组件派发事件

<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.优化事件派发

咱们晓得第2个参数context是一个对象,并且对象中有三个属性attrs,slots,emit,在事件派发的时候,间接应用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进行事件派发
    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函数的执行机会是在beforeCreatecreated之间
  • 因为setup执行机会是在created之间,所以组件才刚刚被创立,而datamethods还没初始化好,所以无奈在setup中应用datamethods
  • setup中this指向undefined
  • setup只能是同步的,不能是异步的

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理