美文网首页
Vue父子组件间传值

Vue父子组件间传值

作者: 异想天不开_9950 | 来源:发表于2019-05-05 11:25 被阅读0次

一、父组件向子组件传值(通过 props 属性)

  • 父组件
<div @touchmove.prevent>
    <datePicker
        ref="startPicker"
        :value="startPickerValue"
        @confirm="startDateConfirm">
    </datePicker>
</div>
import datePicker from '@/components/datePicker';

data() {
    startPickerValue: 0,
},
components: {
    datePicker
},
  • 子组件
<mt-picker ref="picker" :slots="slots" :visible-item-count=7 @change="onValuesChange"></mt-picker>

子组件方法中通过 this.value 来获取数值

/**
 * 组件的属性列表
 */
// 定义父组件传入子组件的值
props: {
    value: null
},
// 这是一个观察属性,父组件传给子组件值后就渲染时间选择器控件
watch: {
    value(val) {
        this.currentValue = val;
        this.setDefaultIndex(this.currentValue);
    }
},
// 设置默认值
setDefaultIndex(res) {

    var time = new Date(res)
    let str = this.setDay(time);
    this.slots[0].defaultIndex = this.dates.indexOf(str);

    var hour = time.getHours()
    this.slots[1].defaultIndex = this.hours.indexOf(parseInt(hour));

    var minute = time.getMinutes()
    if (minute < 10) { minute = '0' + minute }
        this.slots[2].defaultIndex = this.minutes.indexOf(minute);

        var date = this.formatTrans(str, hour+":"+minute); // "2019-04-29 12:04"
        this.currentValue = new Date(date.replace(/-/g, '/'));
    }
},

二、子组件向父组件传值(通过 emit 事件)

需要手动触发获取

  • 子组件
<div @click="confirm">确定</div>
/**
 * 组件的方法列表
 */
confirm() {
    let res = this.$refs.picker.getValues();
    var date = this.dateFormatTrans(res);
    this.currentValue = new Date(date.replace(/-/g, '/'));

    //confirm 事件触发后,自动触发父组件 confirm 事件
    this.$emit('confirm', this.currentValue);
    this.isDisplay = false;
},
  • 父组件

通过 @confirm="startDateConfirm" 把子组件的事件传递给父组件的自定义事件

<div @touchmove.prevent>
    <datePicker
        ref="startPicker"
        :value="startPickerValue"
        @confirm="startDateConfirm">
    </datePicker>
</div>
// 提交事件
startDateConfirm (value) {
    this.startPickerValue = value;
    // 时间选择器确定按钮,并把时间转换成我们需要的时间格式
    var startDate = this.formatStartDateMin(this.startPickerValue).split(' ');

    this.startDayValue = startDate[0];
    this.startMinValue = startDate[1];

    startDate = this.formatTrans(this.startDayValue, this.startMinValue);
    this.startPickerValue = new Date(startDate.replace(/-/g, '/'));
},

相关文章

  • (VUE3) 四、组件传值(父子组件传值 & 祖孙组件传值 &v

    1.父子组件传值 vue2中的父子组件传值:父组件: 子组件: vue3中的父子组件传值: 还是用props接收父...

  • 前端基础搬运工-VUE模块

    十、VUE模块 基础部分 1. Vue组件间传值 答: -[ ] 1.父子之间的传值 父组件向子组件传值通过p...

  • Vue父子组件通信和双向绑定

    本篇文章主要介绍父子组件传值,组件的数据双向绑定。 1. 基础父子组件传值 父子组件传值,这是Vue组件传值最常见...

  • vue 6种通信总结①

    常用vue通信大概有这几种方式进行: 组件间的父子之间的传值 组件间的子父之间的传值 非组件间的组件间的传值(简称...

  • Vue组件间通信

    Vue组件间通信 父子组间通信 通过props向子组件传值,通过事件向父级组件发送消息 通过props向子组件传值...

  • VUE组件(传值,生命周期)

    VUE生命周期 VUE子传父组件通信 VUE非父子组件传值

  • 2020-03月前端面试题

    vue相关 vue父子组件传值方式有哪些? 兄弟组件间如何传值? vuex是用来干什么的? vuex核心模块有哪些...

  • 组件通信

    vue传值可分为父子之间传值、兄弟组件之间传值、跨代组件之间传值 1.父子之间传值:可以使用$emit/props...

  • Vue组件之间的传值

    Vue父子组件之间的传值(props)兄弟组件 VUEX

  • vue组件间传值之eventBus

    1 概述: vue组件间的传值,父子之间props 和emit; 跨组件间可以使用vuex或者eventBus; ...

网友评论

      本文标题:Vue父子组件间传值

      本文链接:https://www.haomeiwen.com/subject/drdcoqtx.html