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

推荐订阅源

S
Security Affairs
S
Secure Thoughts
P
Proofpoint News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Schneier on Security
V
Vulnerabilities – Threatpost
Security Archives - TechRepublic
Security Archives - TechRepublic
T
The Exploit Database - CXSecurity.com
A
Arctic Wolf
Latest news
Latest news
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
T
Troy Hunt's Blog
H
Heimdal Security Blog
美团技术团队
Webroot Blog
Webroot Blog
P
Proofpoint News Feed
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
P
Privacy & Cybersecurity Law Blog
U
Unit 42
Google DeepMind News
Google DeepMind News
V2EX - 技术
V2EX - 技术
G
Google Developers Blog
N
News and Events Feed by Topic
Project Zero
Project Zero
The Register - Security
The Register - Security
N
Netflix TechBlog - Medium
IT之家
IT之家
月光博客
月光博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
L
Lohrmann on Cybersecurity
Schneier on Security
Schneier on Security
博客园_首页
Help Net Security
Help Net Security
AWS News Blog
AWS News Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Security @ Cisco Blogs
PCI Perspectives
PCI Perspectives
Cisco Talos Blog
Cisco Talos Blog
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
D
Docker
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
阮一峰的网络日志
阮一峰的网络日志
Spread Privacy
Spread Privacy
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Hacker News
The Hacker News

小松鼠的博客

记录一次线上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问题排查 K8s中使用Ingress访问请求体过大问题解决 关于k8s中对于SpringBoot应用TCP类型的就绪探针不准确的问题发现 K8s中的环境变量与应用程序的对应关系与操作 SpringMVC4升级为SpringBoot2实战 在Vmware中Ubuntu22.04的vm-tools和网络问题 修改k8s节点主机名并重新加入集群 离线安装Grafana插件 Spring Data Jpa 中使用CriteriaBuilder动态拼接SQL 在SpringBoot项目配置Liquibase数据库版本管理 实现单例模式的8种方法 三种常用的生产者消费者模式实现 使用两个线程交替打印0-100的奇偶数 关于部署于JBoss5中的Spring应用获取项目真实部署路径的问题 获取下一个完全对称日 通过短信验证码验证修改密码的解决方案 在Win10中使用Win+R快速启动软件 使用RSA加解密时注意Cipher.getInstance(String var0,Provider var1)提供的Provider是否正确 在RestEasy2.x中解决接口重复提交问题 几道简单的CTF题目思路 重温Spring---Spring事务控制与基于XML和注解的配置方法 重温Spring---Spring AOP基于XML和注解的配置 重温Spring---AOP动态代理和Spring AOP及其基本原理 重温Spring---Spring IOC基于XML和注解的配置和比较 在Windows10中安装MySQL5.7 Zip版本及常用配置 重温Spring---使用Spring IOC解决程序耦合 策略模式与责任链模式实战应用 Linux上直接打开war包修改文件 在Windows上运行两个微信的简单脚本 ThreadPoolExecutor的使用方法与分页查询数据实例 IDEA中Shelve Changes 和 Git Stash 通过resteasy发布RESTful接口 解决前端请求后台接口,后台报错Can not deserialize instance of java.util.ArrayList out of START_OBJECT token 使用VBA脚本汇总Excel文档 使用Jenkins+GitLab实现自动部署vue项目 Kubernetes:使用hostPath挂载nginx集群的配置文件和html 彻底搞定VirtualBox虚拟机的网络设定 在Docker中安装MySQL5.7并开启远程访问(附授权和修改密码方式) 利用git命令和java文件流 获取自己改动过的文件 浅谈Spring定时任务的使用(Scheduled注解) 在Spring项目简单配置Flyway(V4.2版本)数据库版本管理 解决Spring单元测试中因外键关联导致的失败integrity constraint violation:foreign key no action Redis安装与哨兵模式配置入门 关于Vue中使用Element-UI样式row-class-name失效的问题 Element-UI中实现可动态增加行列和可编辑单元格的表格 Windows系统查看端口占用、结束进程方法和命令 层次分析法(AHP)分析步骤与计算方法 源码分析之解决layui框架重载表格时额外参数不清空的问题 Spring Data Jpa 返回自定义对象(实体部分属性、多表联查) 如何将一个jar放到本地maven仓库中 关于SSM项目停止Tomcat时Log4j出现java.lang.NoClassDefFoundError: 获取el-table单元格值并根据该值对元素自定义样式渲染 解决Git每次push都要重新输入账号密码和HttpRequestException encountered的问题 解决前后端分离项目中Vue不带cookies的问题 SSM集成Shiro自定义权限过滤器不执行解决方案 SSM集成Shiro不进入自定义Realm的doGetAuthorizationInfo的解决方案 Vue+SSM中使用Token验证登录 Git拉代码推送代码提示密码错误如何修改 Git配置SSH Key(Git配置多个账户) 安装Tomcat服务器以及错误汇总(tomcat8.0、jdk8) 关于我
记录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