vue3中setup函数以及setup语法糖父子组件传参

记录vue3中两种setup的传值方法。

setup函数:

父传子:

父组件 :在引入的子组件上绑定下图data属性,值为msg1(属性命名随意,但是要与子组件内接收的保持一致),msg1为所需要传给子组件的值。

子组件 :props接收父组件传的值,与父组件内绑定的属性名一致。在setup函数中,携带第一个参数props,这里面的props即为父组件传来的内容,可以console.log(props)看内容,return出去即可直接使用。


    <!-- setup函数 父传子 -->
        
  <!-- 父组件 -->
<template>
  <div>
    <div>
      <Testview :data="msg1" />
    </div>
  </div>
</template>
<script>
import { ref } from "vue";
import Testview from "../components/TestView.vue";
export default {
  components: {
    Testview,
  },
  setup() {
    const msg1 = ref("setup------函数");
    return {
      msg1
    };
  },
};
</script>

 <!-- 子组件 -->
<template>
  <h1>{{ props.data }}</h1>
</template>
 
<script>
export default {
  props: {
    data: String,
  },
  setup(props) {
    return { props };
  },
};
</script>
子传父:

父组件 :在引入的子组件上绑定 msg事件,内容为value函数(‘msg’的命名随意,但要与子组件mits内的一致),在value函数中的形参即为子组件传入的值。

子组件 :绑定一个方法(btn)点击执行传参。在setup函数中,需要使用第二个参数 context,调用context.emit传递参数,第一个参数为父组件内绑定的事件名,第二个参数为需要传过去的数据。在setup函数外要把传递的事件名写在 emits中,否则会报警告。

注:

setup函数中的两个参数,命名可以随意,但第一个表示接收,第二个表示传递。也就是只用于子传父时,也要把第一个参数带上,否则会报错。

    <!-- setup函数 子传父 -->

     <!--  父组件 -->
<template>
  <div>
    <div>
      <h1>{{ chuan }}</h1>
      <Testview @msg="value" />
    </div>
  </div>
</template>
<script>
import { ref } from "vue";
import Testview from "../components/TestView.vue";
export default {
  components: {
    Testview,
  },
  setup() {
    const chuan = ref(""); 
    const value = (val) => {
      chuan.value = val;
    };
    return {
      chuan,
      value,
    };
  },
};
</script>


    <!--  子组件 -->
<template>
  <button @click="btn()">点击</button>
</template>
 
<script>
import { ref } from "vue";
export default {
  emits: ["msg"],
  setup(props, context) {
    let num = ref(666);
    const btn = () => {
      context.emit("msg", num.value);
    };
    return { props, num, btn };
  },
};
</script>

setup语法糖:

父传子:

父组件 :父组件内的写法与上面setup函数类似,在setup语法糖中,定义的变量不需要return,可直接使用。

 子组件 :子组件需要引入一个新的API:‘defineProps’,用来接收父组件传来的数据。接收来的值不用return,可直接使用。


  <!-- setup语法糖 父传子 -->

  <!-- 父组件 -->
<template>
  <div>
     <hello-world :msg="val"></hello-world>
  </div>
</template>

<script setup>
import { ref } from "vue";
import HelloWorld from "../components/HelloWorld.vue";
let val = ref("setup------语法糖");
</script>

    <!-- 子组件 -->
<template>
  <div>
    <h1>{{ msg }}</h1>
  </div>
</template>

<script setup>
import { defineProps } from "vue";
defineProps({
  msg: String,
});
</script>
子传父:

父组件内的写法与上面setup函数类似。

子组件需要引入一个新的API:‘defineEmits’,用来向父组件传递数据。定义emit时把父组件绑定的事件名写在里面。点击传参时,第一个参数为事件名,第二个参数为传递的数据。


    <!-- setup语法糖 子传父 -->

  <!-- 父组件 -->
<template>
  <div>
     <h1>{{ val }}</h1>
     <hello-world @son="chuan"></hello-world>
  </div>
</template>

<script setup>
import { ref } from "vue";
import HelloWorld from "../components/HelloWorld.vue";
let val = ref("");
let chuan = (v) => {
  val.value = v;
};
</script>

    <!-- 子组件 -->
<template>
  <div>
    <button @click="btn()">点击</button>
  </div>
</template>

<script setup>
import { defineEmits } from "vue";
const emit = defineEmits(["son"]);
const btn = () => {
  emit("son", "123");
};
</script>

遇见问题及解决:

 setup函数在引入组件时,会标红,虽不影响代码运行,但是看着不舒服。

解决办法:找到"Vetur" 插件,禁用即可。

 

  • 11
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vuesetup语法糖,可以通过组件传递参数给子组件,以及通过子组件派发事件传递参数组件。 首先,在组件,我们可以通过在模板使用冒号绑定属性的方式将参数传递给子组件。例如,可以使用`:userType="userType"`将`userType`参数传递给子组件。 然后,在子组件,我们可以使用`defineProps`函数定义接收的参数。在`defineProps`函数,我们可以指定参数的类型、默认值和是否必须传递。例如,`const props = defineProps({ userType: { type: String, default: String, required: true } })`定义了一个名为`userType`的参数,类型为字符串,有一个默认值为空字符串,且必须传递。 在组件,通过派发事件的方式可以获取子组件传入的参数。例如,在组件的方法,可以使用`@menu-send="menuSend"`将子组件通过`$emit`方法派发的事件与组件的方法进行绑定。然后在`menuSend`方法,可以获取子组件传入的参数。 综上所述,通过组件传递参数给子组件和通过子组件派发事件传递参数组件,可以实现父子组件之间的参数传递。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue3 + vite + ts + setup , 第二练 setup ts语法糖实现父子组件传参](https://blog.csdn.net/csl125/article/details/124262027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vue3父子组件传参(setup语法糖写法)](https://blog.csdn.net/skyblue_afan/article/details/126667586)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值