将一个组件的propName属性与父组件中的variable变量进行双向绑定的vue3(组件传值)

封装组件看这个,然后理解父子组件传值

应用场景:

1.使用v - model语法实现双向绑定(传值两边都不用定义方法接数据)

1.子组件
1. @update:modelValue事件是MultiSelect组件对象自带的事件
2.:options="countries"

options是MultiSelect对象自带的可以接受自定义值的属性

countries是我们封装的接受外部自定义值的属性。。引用关系有点复杂

3.代码
复制代码
<template>
  <div class="card flex justify-center">
    <MultiSelect v-model="selectedCountries" :options="countries" optionLabel="name" @update:modelValue="$emit('update:selectedCountries',selectedCountries)" filter placeholder="Select Countries" display="chip" class="w-full md:w-80">

      <template #option="slotProps">
        <div class="flex items-center">
          <div>{{ slotProps.option.name }}</div>
        </div>
      </template>
      <template #dropdownicon>
        <i class="pi pi-map" />
      </template>
      <template #filtericon>
        <i class="pi pi-map-marker" />
      </template>
      <template #header>
        <div class="font-medium px-3 py-2">Available Countries</div>
      </template>
      <template #footer>
        <div class="p-3 flex justify-between">
          <Button label="Add New" severity="secondary" text size="small" icon="pi pi-plus" />
          <Button label="Remove All" severity="danger" text size="small" icon="pi pi-times" />
        </div>
      </template>
    </MultiSelect>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
// import { MultiSelect } from 'primevue/multiselect';
// import { Button } from 'primevue/button';

const props = defineProps({
  // 可以添加自定义的props,例如是否禁用组件等
  disabled: {
    type: Boolean,
    default: false
  },
   countries:{
    type: Array,

     required: true
   },
    selectedCountries:{
      type: Array,

    },
  visible:{
    type: Boolean,
    default: false

  }

});

const selectedCountries = ref([])
// 如果需要根据父组件传入的值改变内部数据结构,可以在这里处理
// 例如:
// if (props.someValue) {
//     // 对countries或者selectedCountries进行操作
// }

// 可以定义内部的方法,如果需要暴露给父组件,可以使用emits
const emit = defineEmits(['update:selectedCountries']);
const emitModelValueChange = () => {
  emit('update:modelValue', selectedCountries);
};
</script>
2.父组件
1.selectedCountries这个变量我们没有在父组件里定义,这个是跟在事件之后的参数,也是我们要传的值
2.selectedUsers这个变量就是父组件所属的了

3.界面效果

2.手动实现双向绑定(不使用v - model语法的传统方式)(传值两边都需要定义方法接数据)

1.子组件
复制代码
<template>
  <div class="card flex justify-center">
<MultiSelect v-model="selectedCountries" :options="countries" optionLabel="name" @update:modelValue="emitModelValueChange" filter placeholder="Select Countries" display="chip" class="w-full md:w-80">
      <template #option="slotProps">
        <div class="flex items-center">
          <div>{{ slotProps.option.name }}</div>
        </div>
      </template>
      <template #dropdownicon>
        <i class="pi pi-map" />
      </template>
      <template #filtericon>
        <i class="pi pi-map-marker" />
      </template>
      <template #header>
        <div class="font-medium px-3 py-2">Available Countries</div>
      </template>
      <template #footer>
        <div class="p-3 flex justify-between">
          <Button label="Add New" severity="secondary" text size="small" icon="pi pi-plus" />
          <Button label="Remove All" severity="danger" text size="small" icon="pi pi-times" />
        </div>
      </template>
    </MultiSelect>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
// import { MultiSelect } from 'primevue/multiselect';
// import { Button } from 'primevue/button';

const props = defineProps({
  // 可以添加自定义的props,例如是否禁用组件等
  disabled: {
    type: Boolean,
    default: false
  },
   countries:{
    type: Array,

     required: true
   },
    selectedCountries:{
      type: Array,

    },
  visible:{
    type: Boolean,
    default: false

  }

});

const selectedCountries = ref([])
// 如果需要根据父组件传入的值改变内部数据结构,可以在这里处理
// 例如:
// if (props.someValue) {
//     // 对countries或者selectedCountries进行操作
// }

// 可以定义内部的方法,如果需要暴露给父组件,可以使用emits

//updateSelectedCountries名字随便,爱叫是什么叫什么,只要统一就行

const emit = defineEmits(['updateSelectedCountries']);
const emitModelValueChange = () => {
  emit('updateSelectedCountries', selectedCountries);
};
</script>
2.父组件
3.界面效果

3.为啥一定要传值呢?

因为不同的vue文件要传值,

那为什么不直写在一个组件里?

因为抽出一个组件后,可以复用组件,减少代码量,代码看起来整洁不少。。。。。。。

相关推荐
linux_cfan41 分钟前
videojs v10 源代码系列解读:14 · 谓词守卫:在运行时安全地调用能力
前端·javascript·音视频
+VX:Fegn08952 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
雪芽蓝域zzs2 小时前
第 7 章:双 Y 轴组合图(柱状 + 折线,看板高频场景)
vue.js·echars
一 乐2 小时前
二手交易平台|基于springboot + vue二手交易平台(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序
天若有情6733 小时前
【纯前端小工具】公历生日转农历,批量查询每年农历生日对应的公历日期(GitHub Pages在线直接用)
前端·javascript·github pages·农历转换·lunisolar·网页小工具
vx-程序开发3 小时前
【计算机毕设】django校园跑腿服务系统83141
java·数据库·vue.js·spring boot·spring·elasticsearch·django
林语琛4 小时前
我写的 switch…break 被 Babel 偷偷吞了
前端·javascript·babel
liuchangng4 小时前
Jev 模型研究:从生成式大模型到决策式模型——System One、RLCD 校准与采用边界
java·javascript·人工智能·python·深度学习
一 乐4 小时前
养老院管理系统|基于springboot + vue养老院管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·毕业设计
我命由我123455 小时前
CSS - CSS 媒体查询 orientation
前端·javascript·css·html·css3·html5·js