简体   繁体   English

未定义的数据从子组件传递到父组件

[英]Data passed as undefined from child to parent component

I am making a file selection in the child component and once file is selected I want to pass the selected file to the parent component.我正在子组件中进行文件选择,一旦选择了文件,我想将选定的文件传递给父组件。 I am not able to figure out how to do that.我不知道该怎么做。 Please help out.请帮忙。 If I try to get the print the value inside the onDocumentSelected(value) it comes out to be undefined.如果我尝试打印 onDocumentSelected(value) 中的值,则结果是未定义的。

Error message Property or method "value" is not defined on the instance but referenced during render错误消息属性或方法“值”未在实例上定义,但在渲染期间被引用

Parent Component父组件

<template>
  <v-form :model='agency'>
    <DocumentSelect 
       type="file"
       ref="file"
       :required="true"
       name="vue-file-input"
       @change="onDocumentSelected(value)"
    />
  </v-form>
</template>

<script>
  import DocumentSelect from 'views/document-selection.vue';

export default {
  components: {
    DocumentSelect
  },
  props: ['agency'],
  methods: {
    onDocumentSelected(value) {
      console.log(value); //undefined
    },
  }
};
</script>

Child Component子组件

<template>
  <div class="input-group input-group--select primary--text" :class="{'input-group--required': required, 'input-group--error': hasError, 'error--text': hasError}" >
     <div class="input-group__input">
       <input
          type="file"
          name="name"
          ref="file"
          @change="onDocumentSelected" />
     </div>
      <div class="input-group__details">
        <div class="input-group__messages input-group__error" v-for="error in errorBucket">
          {{error}}
        </div>
      </div>
   </div>
</template>

<script>
  import Validatable from  'vuetify/es5/mixins/validatable.js'

  export default {
    mixins: [Validatable],
    props: ['type', 'name', 'required'],
    data: function () {
      return {
        inputValue: ''
      };
    },
    watch: {
      inputValue: function(value) {
        this.validate();
        console.log(value); // *Event {isTrusted: true, type: "change", target: input, currentTarget: null, eventPhase: 0, …}*
        this.$emit('change', {value});
      },
    },
    methods: {
      onFileSelected(event) {
        this.inputValue = event
      },
    },
  };
</script>

Get rid of your watch method and move the logic inside onFileSelected :摆脱你的 watch 方法并移动onFileSelected内部的逻辑:

@change="onFileSelected($event)"
onFileSelected(e) {
  this.validate();
  this.$emit('document-selected', e);
}

Then, in the parent, listen for the document-selected event:然后,在父级中,监听document-selected事件:

<DocumentSelect 
       type="file"
       ref="file"
       :required="true"
       name="vue-file-input"
       @document-selected="onDocumentSelected($event)"
/>

You then have access to that value to do what you want with.然后,您可以访问该值来做您想做的事情。

暂无
暂无

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

相关问题 React中从父组件传递给子组件的“未定义”? - 'Undefined' passed from parent to child component in React? 从 Angular 父组件传递给子组件的数据在子组件中未定义 - Data passed from an Angular parent component to a child component is undefined in child component 当单击从父组件传递时,为什么子组件未定义“this”? - Why 'this' is undefined for child component, when click is passed from a parent component? 从子组件传递数据时,父州未更新 - Parent State not updating when passed data from child component 如何访问从父组件传递到子组件的属性的某些数据? - How do I access some data of a property I passed from parent component to child component? TypeError: Cannot read property 'map' of undefined 当我尝试 map 通过从父组件传递到子组件的道具时显示 - TypeError: Cannot read property 'map' of undefined is showing when I'm trying to map over the props passed from parent component to child component 如何测试回调单击从父组件传递的子组件? - How test callback click in child component that was passed from parent component? React 子组件不会更新从父组件传递的属性 - React Child Component does not update properties passed from Parent Component 虽然子组件道具是从父组件传递的,但它们是空的 - Child component props are empty though they're passed from a parent component vue 3访问从父组件传递的未在子组件中定义的道具 - vue 3 access props passed from parent component that are not defined in child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM