当前位置: 首页 > news >正文

将一个组件的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文件要传值,

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

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


http://www.mrgr.cn/news/81598.html

相关文章:

  • 工业相机如何降低CPU占用率
  • 猴子吃桃问题
  • 帝国CMS8.0终极栏目转换或批量改顺序成功后不能返回地址的解决方案
  • 定位的叠放次序 z-index
  • Gradle国内镜像地址
  • 书生大模型基础岛第四关
  • SpringCloudAlibaba实战入门之路由网关Gateway初体验(十)
  • 【可靠有效】springboot使用netty搭建TCP服务器
  • 《机器学习》从入门到实战(1)
  • 《机器学习》——KNN算法
  • QT集成intel RealSense 双目摄像头
  • 新浪微博C++面试题及参考答案
  • 细说EEPROM芯片24C02的基础知识及其驱动程序设计
  • 【达梦数据库】小版本升级之bin文件替换
  • 是德 皮安表Keysight B2980 系列常用指令 附带说明书原件
  • E-commerce .net+React(一)——项目初始化
  • Java数组深入解析:定义、操作、常见问题与高频练习
  • 高性能编程,C++的无锁和有锁编程方法的性能对比
  • 2023 年 12 月青少年软编等考 C 语言四级真题解析
  • 字节跳动Java开发面试题及参考答案(数据结构算法-手撕面试题)
  • Anaconda搭建Python虚拟环境并在Pycharm中配置(小白也能懂)
  • 【物联网技术与应用】实验16:模拟霍尔传感器实验
  • YOLOv9-0.1部分代码阅读笔记-detect.py
  • 高精度问题
  • H5st5.0.0协议分析
  • 穷举vs暴搜vs深搜vs回溯vs剪枝系列一>括号生成