惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
G
Google Developers Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
有赞技术团队
有赞技术团队
Vercel News
Vercel News
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
I
InfoQ
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
The Cloudflare Blog
B
Blog
C
Check Point Blog
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
U
Unit 42
D
Docker
月光博客
月光博客
aimingoo的专栏
aimingoo的专栏
博客园 - Franky
A
About on SuperTechFans

小松鼠的博客

记录一次线上k8s工作节点无法创建容器的问题排查思路与解决办法 记一次线上GoLang项目OOM排查过程 从LastPass转向拥抱开源KeePass的心路历程 故障定位与 AI 结合前后端编码实践 FileBeat收集nginx-ingress-controller日志 K8s云原生环境下文件描述符占用过高查询思路 2024年最新关闭火绒安全工具的开机自启方法 Kubernetes任务调度实践-Go语言实现Job和CronJob对比分析 离线更新k8s环境下的trivy漏洞库方法 使用Go语言接入Choerodon实现基于OAuth2的统一身份认证登录 在Vue2中自定义Switch组件并实现父子组件双向数据绑定 关于docker jdk1.8镜像中的GB18030-2022标准支持及验证 Go框架gin中的session存储gin-contrib-sessions和go-session 关于修改node_module中的源码问题记录 docker-compose网络和内网服务IP冲突问题 慎用存储过程:一条语句引发的数据库存储100%占用 Spring Boot中4种文件下载方法的实现 避坑-不能将specific类型的gitlab-runner改变为share类型 Docker compose中的MySQL主从复制模式和percona-toolkit工具使用 在minio中开启https访问以及使用rclone备份minio桶 在多机Docker环境下部署Choerodon的解决方案 Prometheus中Monitor添加对SpringBoot Actuator的Basic认证 在Nginx的容器镜像中隐藏Nginx的Server响应头 K8s中的两种nginx-ingress-controller及其区别 两个docker工具:runlike和whaler Grafana中的邮件报警和截图插件grafana-image-enderer K8s中externalName-service和services-without-selectors maven配置文件settings.xml中的一些概念总结 K8s中flexvolume插件驱动的安装 K8s中的coredns无法解析svc问题排查
记录Vue中父子组件传值的实战应用
ycyin · 2022-02-15 · via 小松鼠的博客

2022年2月15日大约 4 分钟前端技术vuepropsemit


背景

在vue中只要用到子组件就会涉及到父子组件的传值,以下面这个例子为引导,记录Vue中父子组件的通信传值问题,防遗忘。为了简洁只记录一种最为常用的方式。其它方式可以参考文末的参考文章。

使用vant提供的van-popup组件时,默认样式不满足项目需求,需要自定义。我们就对van-popup进行二次封装其中会用到父子组件传值。

父组件给子组件传值

在子组件中定义props,包含父组件要给子组件传递的属性值名称、类型、默认值等。

export default {
  name: "RadioListPicker",
  props: {
    title:{
      type: String,
      default: null
    },
    columns: {
      type: Array,
      default: null
    },
  },
  data(){
    return {
    }
  },
  methods:{

  },
  watch:{

  }
}

当父组件传递过来值后,props中的参数被解析为整个子组件域可用,所以子组件可以直接使用props中的属性即可。

<div class="radio-picker__title van-ellipsis">{{title}}</div>
<van-cell v-for="item in columns" :key="item.key" :title="item.value" @click="handleClick($event,item.key,item.value)"/>

在父组件中使用子组件时传递给它对应的值即可。以下例子

<RadioPicker  :title="title" :columns="data" ></RadioPicker>

这样在父组件中传递给子组件的可变参数title、data子组件使用title、columns可成功接收。

子组件给父组件传值

子组件给父组件传值,需要在父组件使用子组件时定义好回调函数(个人理解,可能描述不准确),在子组件中使用$emit触发。通过给这个回调函数传递参数即可达到子组件给父组件传值的目的。

以下示例:

父组件:定义子组件会通过$emit触发handleClickhandleClose

<RadioPicker @handleClick="onClick" @handleClose="close"></RadioPicker>

子组件:通过$emit触发handleClickhandleClose,并可以传值

/**
 * 点击Item事件
 */
handleClick(e, key,value){
  this.$emit('handleClick',{key:key,value:value})
},
/**
 * 弹出层关闭时触发@close
 */
close(){
  this.$emit('handleClose')
}

完整实例:

子组件: 是对vant组件库的van-popup组件的进一步封装。添加了顶部toolbar和关闭按钮,底部是一个可以可以点击选择的list列表。

<template>
  <div>
    <van-popup :show="showPopup" round position="bottom" @close="close">
      <van-row class="radio-picker__toolbar">
        <van-col span="16" offset="4">
          <div class="radio-picker__title van-ellipsis">{{title}}</div>
        </van-col>
        <van-col span="4">
          <button type="button"  class="radio-picker__cancel van-haptics-feedback" @click="cancel">
            <van-icon class-prefix="iconfont icon-blue-close"/>
          </button>
        </van-col>
      </van-row>
      <div class="radio-picker__content" :style="'height:'+height+'px'">
        <van-cell v-for="item in columns" :key="item.key" :title="item.value"
                  @click="handleClick($event,item.key,item.value)"/>
      </div>
    </van-popup>
  </div>
</template>

<script>

export default {
  name: "RadioListPicker",
  props: {
    show: {
      type: Boolean,
      default: false
    },
    height: {
      type: Number,
      default: 300
    },
    title:{
      type: String,
      default: null
    },
    columns: {
      type: Array,
      default: null
    },
  },
  data(){
    return {
      showPopup: this.show
    }
  },
  methods:{
    /**
     * 点击Item事件
     */
    handleClick(e, key,value){
      this.$emit('handleClick',{key:key,value:value})
    },
    /**
     * 右上角×点击关闭
     */
    cancel(){
      this.showPopup = false;
    },
    /**
     * 弹出层关闭时触发@close
     */
    close(){
      this.$emit('handleClose')
    }
  },
  watch:{
    /**
     * watch props参数show,及时响应显示与关闭
     */
    show(){
      this.showPopup = this.show
    },

  }
}
</script>

<style lang="less" scoped>
@import "../../less/color";

  .radio-picker__toolbar{
    align-items: center;
    height: 51px;
    background-color: @bgGrayLightColor;

    .radio-picker__cancel{
      height: 100%;
      padding: var(--van-padding-md);
      font-size: var(--van-font-size-md);
      background-color: transparent;
      border: none;
      color: @textBlueColor;
      font-size: 14px;
    }

    .radio-picker__title {
      max-width: 100%;
      font-size: 16px;
      line-height: 51px;
      font-family: @fontFamilyBold;
      font-weight: 600;
      color: @textBlackColor;
    }
  }

  .radio-picker__content{
    height: 300px;
    overflow-y: auto;

    :deep(.van-cell__title){
      font-size:14px;
      font-family: @fontFamilyRegular;
      font-weight: 400;
      color: @textBlackDeepColor;
    }
  }

</style>

父组件: 使用子组件,给子组件传值、接收子组件选择的值。

<template>
  <div class="home">
    <button class="btns" @click="this.showPicker = true">显示单个Picker</button>
    <RadioPicker :show="showPicker" :title="title" :columns="data" :height="300"
                 @handleClick="onClick" @handleClose="close"></RadioPicker>
  </div>
</template>

<script>
import RadioPicker from '../components/common/RadioListPicker.vue'

export default {
  name: 'Home',
  data() {
    return {
      showPicker:false,
      title:"请选择",
      data:[
        {key:1,value:'xxx'}, {key:2,value:'xx'}, {key:10,value:'xxxxxx'},
        {key:14,value:'xxx'}, {key:15,value:'xxxxx'}, {key:16,value:'xxx'},
        {key:4,value:'xxx'},{key:20,value:'xxxxxx'},{key:26,value:'xxxxxxxxxxx'},{key:3,value:'xx'}]
    }
  },
  components: {RadioPicker},
  methods: {
    /**
     * @handleClick处理列表点击事件
     * @param key
     * @param value
     */
    onClick({key,value}){
      this.showPicker = false;
      console.log("父组件接收到值")
      console.log(key,value)
    },
    /**
     * @handleClose处理关闭事件
     */
    close() {
      this.showPicker = false;
    }
  }
}

</script>

参考:

Popup 弹出层 - Vant 3 (gitee.io)

单文件组件 | Vue.js (vuejs.org)

应用 & 组件实例 | Vue.js (vuejs.org)

实例方法 | Vue.js (vuejs.org)

Vue 自定义组件 - 简书 (jianshu.com)

vant组件slot使用,vue实现单选多选_qq_42301244的博客-CSDN博客_v-slot:search

vant-picker实现自定义内容,根据内容添加图标_Ponnenult的博客-CSDN博客_vant-picker