简体   繁体   English

将数据从子组件传输到父组件

[英]Transfer data from child component to parent component

I have a form (parent) component with two input text fields and date field for which vuejs-datepicker component is used (child component. See below:我有一个表单(父)组件,其中包含两个输入文本字段和使用 vuejs-datepicker 组件的日期字段(子组件。见下文:

Form component表单组件

<template>
<div>
    <form>
        Name<input type="text" v-model="name"/><br>
        Age <input type="text" v-model="age"/><br>
        <calendar2 @wybrano="zm($event)"></calendar2>
        <button type="button" @click="sendfo()">Wyslij</button>
    </form>
</div>
</template>

<script>
export default {

    data() {
        return {
            name:"",
            age:"",  
            da:""
        }
    },
    created() {


    },
    methods: {
        zm: function(ev) {
            this.da=ev
        },

        sendfo: function() {
            var t = this
            axios({
                method:"POST",
                url: "sendfo",
                data: {
                    name:"messi",
                    age:32,
                    data:t.da

                },
            }).then(function(response) {
                console.log('done');
            })



        }

    }
}
</script>

Calendar component日历组件

<template>
<div>
<datepicker v-model="data" name="data" :format="ff" :monday- 
first="mondayFirst" :clear-button="clearButton" :calendar- 
button="calendarButton" @selected="sel"></datepicker>
</div>
</template>

<script>
import Datepicker from 'vuejs-datepicker';
 export default {
components: {
  Datepicker
},
created() {


},


data() {
  return {
    data: "",
    mondayFirst: true,
    clearButton: true,
    calendarButton: true,
  }
},
methods: {
  sel: function() {
    if(this.data){
    this.data=this.data.toISOString().substr(0,10)
    this.$emit('wybrano', this.data)
    }
    },


    }
  }
</script>

My purpose is to use form component to collect form data and send them via ajax to database.我的目的是使用表单组件来收集表单数据并通过 ajax 将它们发送到数据库。 As you can see in the code I use $emit to transfer data from calendar component to the parent component.正如您在代码中看到的,我使用$emit将数据从日历组件传输到父组件。 Everything work almost fine but... Selected (by user) date in calendar component is different than this date transferred to the parent component.一切正常,但是......日历组件中选择的(由用户)日期与传输到父组件的日期不同。 I give few examples;我举几个例子;

date selected in calendar 07/08/2018 -> var data="07/08/2018" -> var da=""
date selected in calendar 09/12/2018 -> var data="09/12/2018" -> var da="07/08/2018"
date selected in calendar 01/01/2018 -> var data="01/01/2018" -> var da="09/12/2018"

I hope my problem is understandable.我希望我的问题是可以理解的。 Every time var da in parent component takes value from previous selection.每次父组件中的 var da从先前的选择中获取值。 Please help me solve the problem.请帮我解决问题。

First thing I would try is change these two lines so you don't change the bound model value:我要尝试的第一件事是更改这两行,这样您就不会更改绑定模型值:

var payload=this.data.toISOString().substr(0,10)
this.$emit('wybrano', payload)

This issue is due to the fact that the selected event is propagated before the data property gets updated (thus causing the previous selected value to be emitted).此问题是由于所选事件在数据属性更新之前传播(从而导致发出先前选择的值)。

To be sure to propagate the selected date once it is updated, you could instead use a watcher method to observe changes for the data property.为了确保在更新后传播所选日期,您可以改为使用观察者方法来观察数据属性的更改。

Then, in the watcher method, you would emit your event as already done.然后,在 watcher 方法中,您将像已经完成的那样发出您的事件。

So in the ///Calendar component you would have something like this, where @selected is removed:因此,在///Calendar组件中,您将拥有类似这样的内容,其中删除了 @selected:

<datepicker v-model="data" name="data" :format="ff" :monday- 
  first="mondayFirst" :clear-button="clearButton" :calendar- 
  button="calendarButton"></datepicker>
///
 methods: {
   ...
   watch: {
     'data' () {
       this.$emit('wybrano', this.data.toISOString().substr(0,10))
     }
   }

Then, I would advice you to use meaningful functions, variable names, in order to ease code readability and maintenance.然后,我建议您使用有意义的函数、变量名,以简化代码的可读性和维护。

For example, you could rename Calendar data to someDate (some needs to be clarified).例如,您可以将日历数据重命名为someDate (有些需要澄清)。 wybrano event named would be renamed to *someDateUpdated. wybrano事件命名将重命名为 *someDateUpdated。 zm function name also is not clear. zm函数名也不清楚。

You could try using v-model on the calendar component.您可以尝试在日历组件上使用v-model

With this approach VueJS will may have the good reactivity to the selection of the date.使用这种方法,VueJS 可能会对日期的选择产生良好的反应。

Documentation: Using v-model on Components文档: 在组件上使用 v-model

/// Form component /// 表单组件

...
<calendar2 v-model="da" />
...

/// Calendar component /// 日历组件

... in the template
<datepicker v-model="value" input="$emit('input', $event.target.value.toISOString().substr(0,10))" ... />
...

... in the script
data() { /* datepicker configuration */ },
props: ['value'],
...

Maybe it will be enough to change this line:也许改变这一行就足够了:

this.$emit('wybrano', this.data)

like this:像这样:

this.$nextTick(this.$emit('wybrano', this.data))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM