简体   繁体   English

Vue.js - 将值从父级传递给子级不起作用(如预期的那样)

[英]Vue.js - passing value from parent to child does not work (as expected)

I pass the value from parent template to child template under this scheme:在此方案下,我将值从父模板传递给子模板:
parentModel -> parentTemplate -> prop -> childModel -> childTemplate. parentModel -> parentTemplate -> prop -> childModel -> childTemplate。

That is, when getting in a child model, I need to handle value before installing in template... but it doesn't work!也就是说,当进入一个孩子 model 时,我需要在模板中安装之前处理值......但它不起作用!

My method is similar to a kludge =(我的方法类似于kludge =(

Parent:家长:

<template>
  <section class="login-wrapper border border-light">
      <form id="add-form" class="form-signin" enctype="multipart/form-data" @submit.prevent="send">
        <label>
          <span>Images</span>
          <input type="file" id="files" ref="files" multiple @change="addFile()"/>
        </label>
        <button type="submit">Submit</button>
      </form>
      <div id="files-container">
        <div v-for="(file, index) in files" class="file-listing" v-bind:key="index">
          <Preview :msg="file"></Preview><!-- here I send data to the child with :msg property -->
        </div>
      </div>
  </section>
</template>


<script>
import Preview from "../Partial/ImagePreview.vue"


export default {
  name: "ProductAdd",
  components: {
    Preview
  },
  data() {
    return {
      files: []
    }
  },
  methods: {
    addFile() {
      for (let i = 0; i < this.$refs.files.files.length; i++) {
        const file = this.$refs.files.files[i]
        this.files.push( file );
      }
    },
    async send() {
      /// Sending data to API
    }
  }
}
</script>

Child:孩子:

<template>
  <section>
    <span>{{ setImage(msg) }}</span><!-- This I would like to avoid -->
    <img :src="image_url" alt=""/>
  </section>
</template>


<script>
export default {
  name: 'ImagePreview',
  data: () => {
    return {
      image_url: ""
    }
  },
  props: [ "msg" ],
  methods: {
    setImage(data) {
      const reader = new FileReader();
      reader.onload = (event) => {
        this.image_url = event.target.result;
      };
      reader.readAsDataURL(data);
      return null;
    }
  }
}
</script>

I'm so sorry for a stupid question (perhaps), but I rarely work with frontend.对于一个愚蠢的问题我很抱歉(也许),但我很少使用前端。 Now there is such a need =)现在有这样的需求=)

PS: I tried using "watch" methods, it doesn't work in this case. PS:我尝试使用“watch”方法,在这种情况下不起作用。 When changing an array in the parent component, these changes are not passed to child在父组件中更改数组时,这些更改不会传递给子组件

But its work.. I see selected image preview但它的工作..我看到选定的图像预览

 const Preview = Vue.component('ImagePreview', { data: () => { return { image_url: "" } }, template: ` <section> <span>{{ setImage(msg) }}</span><:-- This I would like to avoid --> <img,src="image_url" alt=""/> </section> `: props, [ "msg" ]: methods; { setImage(data) { const reader = new FileReader(). reader.onload = (event) => { this.image_url = event.target;result; }. reader;readAsDataURL(data); return null; } } }): new Vue({ name, "ProductAdd": components, {Preview}: data() { return { files, [] } }: methods; { addFile() { for (let i = 0. i < this.$refs.files.files;length. i++) { const file = this.$refs.files.files[i] this.files;push( file ), } }. async send() { /// Sending data to API } } });$mount('#container');
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id='container'> <section class="login-wrapper border border-light"> <form id="add-form" class="form-signin" enctype="multipart/form-data" @submit.prevent="send"> <label> <span>Images</span> <input type="file" id="files" ref="files" multiple @change="addFile()"/> </label> <button type="submit">Submit</button> </form> <div id="files-container"> <div v-for="(file, index) in files" class="file-listing" v-bind:key="index"> <Preview:msg="file"></Preview><:-- here I send data to the child with :msg property --> </div> </div> </section> </div>

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

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