Vue3 Setup语法糖,父子组件之间传各种数据类型方法

Vue3 Setup语法糖,父子组件之间传各种数据类型方法

一、父组件向子组件使用语法糖传字符串类型

        1.父组件

msg是你的自定义的变量名,用于传给子组件,content是你父组件的要传给子组件的数据。

<template>
  <div>
    <Hello :msg="content" />
  </div>
</template>

<script setup>
import { ref } from "vue";
import Hello from "../components/HelloWorld.vue";
const content = ref("那就这样吧");
</script>

        2.子组件

语法糖中可以直接在props = defineProps()的方法里面写你穿过来的数据,最后在模板中调用就ok了

<template>
  <div>
    <h1>{{ props.msg }}</h1>
  </div>
</template>

<script setup>
const props = defineProps({
  msg: String,
});
</script>

 最终呈现处这样的效果

二、父组件向子组件使用语法糖传数组类型

1.父组件传递

 组件中DateList是你声明传递给子组件的名称,ArrayList是你要传递给子组件的数据。

<template>
  <div>
    <Hello  :DateList="ArrayList"  />
  </div>
</template>

<script setup>
import { reactive } from "vue";
import Hello from "../components/HelloWorld.vue";
const ArrayList = reactive([
  {
    id: 1,
    name: "法外",
  },
  {
    id: 2,
    name: "狂徒",
  },
  {
    id: 3,
    name: "张三",
  },
]);
</script>

2.子组件接收

语法糖中可以直接在props = defineProps()的方法里面写你穿过来的数据,最后在模板中循环输出就ok了

<template>
  <div>
    <h1>{{ props.msg }}</h1>
    <div v-for="(item, index) in props.DateList" :key="item.id">
      {{ item.name }}
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  msg: String,
  DateList: Array,
});
console.log(props.DateList);
</script>

呈现效果

三.子组件向父组件使用语法糖传字值

1.子组件

 子传父需要用到事件来传递, let emigFun = defineEmits(["handLer"]); 使用defineEmits语法糖,创建一个自定义事件,然后根据自定义事件来传递你想要传递的类型(如下图)

<template>
  <div>
    <div @click="fun">
        子传父
    </div>
  </div>
</template>

<script setup>
import { reactive } from "vue";
let emigFun = defineEmits(["handLer"]);//使用defineEmits语法糖的方法来创建自定义事件。
const dat = reactive([
  {
    name: 123,
    id: 1,
  },
  {
    name: 123,
    id: 1,
  },
  {
    name: 123,
    id: 1,
  },
  {
    name: 123,
    id: 1,
  },
]);
const fun = () => {
  emigFun("handLer", dat);//左边是你的自定义事件名,右边是你要传的数据。
};
</script>

2.父组件

父组件这边就可以直接使用你子组件创建的自定义事件,加上你父组件的事件名,通过形参的方式成功拿到子组件的数据(如下图)

<template>
  <div>
    <Hello  @handLer="hand" />
    <div>
        点击拿到子组件的值
    </div>
  </div>
</template>

<script setup>
import Hello from "../components/HelloWorld.vue";
const hand = (v) => {
  console.log(v);
};
</script>

效果展示

总结

另外还有一些使用setup语法糖的一些问题,下一篇给大家讲一下,其次Vue父子组件互相传值,传来传去新手容易懵,多理清思路,慢慢来,还有什么更好的见解,或者想法请在下方评论探讨。

 

  • 19
    点赞
  • 99
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Vue3中,使用`setup`语法糖来编写组件,可以将组件的逻辑和模板分离,更加清晰和易于维护。以下是一个简单的例子,包括父子组件之间的输入框值: 组件template代码: ```html <template> <div> <p>输入的值是:{{ value }}</p> <child-component :prop-value="value" @update-value="handleUpdate"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { value: '' } }, methods: { handleUpdate(value) { this.value = value; } } } </script> ``` 在组件中,我们引入了子组件,并将组件的`value`属性传递给了子组件的`prop-value`属性。我们还在子组件上绑定了一个`update-value`事件,用于接收子组件传递过来的值。 子组件template代码: ```html <template> <input type="text" v-model="inputValue" @input="updateValue"> </template> ``` 在子组件中,我们使用了`v-model`来双向绑定输入框的值,并在输入框的`input`事件中触发了`updateValue`方法。 子组件script代码: ```js <script> export default { props: { propValue: { type: String } }, setup(props, { emit }) { const inputValue = Vue.ref(props.propValue); function updateValue() { emit('update-value', inputValue.value); } return { inputValue, updateValue } } } </script> ``` 在子组件的`setup`函数中,我们使用了`props`参数来获取组件传递过来的`prop-value`属性,使用了`emit`函数来触发`update-value`事件,并将输入框的值作为参数传递组件。 这样,我们就实现了父子组件之间的输入框值。组件可以通过属性将组件的值传递给子组件,子组件可以通过事件将输入框的值传递组件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值