Vue3笔记 - setup的两个注意点

6.setup的两个注意点

  • setup执行的时机

    • 在beforeCreate之前执行一次,this是undefined。
  • setup的参数

    • props:值为对象,包含:组件外部传递过来,且组件内部声明接收了的属性。
    • context:上下文对象
      • attrs: 值为对象,包含:组件外部传递过来,但没有在props配置中声明的属性, 相当于 this.$attrs
      • slots: 收到的插槽内容, 相当于 this.$slots
      • emit: 分发自定义事件的函数, 相当于 this.$emit
<template>
  <Demo msg="你好啊" school="尚硅谷" @hello="showHelloMsg">
    <template v-slot:qwe>
      <span>尚硅谷</span>
    </template>
    <template v-slot:sdf>
      <span>尚硅谷2</span>
    </template>
  </Demo>
</template>



<script>
/*  
  setup执行时机:
    在beforeCreate之前执行一次,this是undefined
  setup的参数
    props:值为对象,包含:组件传递过来,且组件内部声明接收了的属性
    context:上下文对象
      attrs:值为对象,包含:组件外部传递过来,但没有在props配置中声明的属性, 相当于this.$attrs
      slots:收到的插槽内容,相当于this.$slots
      emit:分发自定义事件的函数,相当于this.$emit.
*/ 
import Demo from "./components/Demo.vue";
export default {
  name: "App",
  components: {
    Demo,
  },
  setup() {
    function showHelloMsg(value) {
      console.log(1111);
      alert(`你好啊,你触发了hello事件,我收到的参数是:${value}`);
    }
    return {
      showHelloMsg,
    };
  },
};
</script>

总结一下:

  1. setup执行时机:在beforeCreate之前执行,this是undefined
  2. setup的参数:
    • props:接收组件外部传递过来的数据,组件内部必须声明接收(传递未接收,有警告,接收未传,无警告)
    • context:上下文对象
      • (attrs(组件外部传递过来没有接收的属性)–> this.$attrs
      • slots(接收插槽内容)–> this.$slots
      • emit (自定义事件的函数)–>this.$emit)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值