简体   繁体   English

两个子组件之间的数据传输问题

[英]The problem of data transfer between two child components

Good afternoon, I have two child components Header and Pagination. 下午好,我有两个孩子部分:页眉和分页。 In Header, I have an input search engine and two inputs (title and body) in order to be able to add a post to Pagination. 在页眉中,我有一个输入搜索引擎和两个输入(标题和正文),以便能够在分页中添加帖子。 I managed to transfer the search value to the Pagination component, but I don't know how to transfer the value from two inputs (title, body). 我设法将搜索值转移到“分页”组件,但是我不知道如何从两个输入(标题,正文)转移值。 I use to transfer the event bus. 我用来转移事件总线。 Help me please pass the value of the two inputs (title, body) into the Pagination component when you click the AddPost button. 帮我,请在单击AddPost按钮时将两个输入(标题,正文)的值传递到分页组件中。

My code on GitHub 我在GitHub上的代码

Screenshot of app 应用的屏幕截图

My code of component Header: 我的组件头代码:

    <template>
<div class="header">
  <input type="text" v-model="search" class="header_input_search" placeholder="Search" @input="saveMessage" />
  <img src="src/assets/milk.png">
  <div class="header_div_inputs">
    <input type="text" v-model="createTitle" class="created"/>
    <p><input type="text" v-model="createBody" class="createBody"/></p>
  </div>
  <button  @click="addPost()" class="addPost">AddPost</button>
</div>
</template>

<script>
  import axios from 'axios';
  import {eventEmitter} from './main'

  export default {
    name: 'Header',
    data () {
      return {
        search: '',
        createTitle: '',
        createBody: '',
      }
    },
    methods:{
      saveMessage(){
        eventEmitter.$emit('messageSave', this.search)
      },
    }
  }
</script>

My code of component Pagination: 我的组件分页代码:

<template>
  <div class = "app">
    <ul>
      <li v-for="(post, index) in paginatedData" class="post" :key="index">
        <router-link :to="{ name: 'detail', params: {id: post.id, title: post.title, body: post.body} }">
        <img src="src/assets/nature.jpg">
        <p class="boldText"> {{ post.title }}</p>
        </router-link>
        <p> {{ post.body }}</p>
      </li>
      </ul>
        <div class="allpagination">
          <button type="button" @click="page -=1" v-if="page > 0" class="prev"><<</button>
          <div class="pagin">
            <button class="item"
            v-for="n in evenPosts"
            :key="n.id"
            v-bind:class="{'selected': current === n.id}"
            @click="page=n-1">{{ n }} </button>
          </div>
          <button type="button" @click="page +=1" class="next" v-if="page < evenPosts-1">>></button>
        </div>
      </div>
    </template>

    <script>
      import {mapState} from 'vuex'
      import {eventEmitter} from './main'
      export default {
        name: 'app',
        data () {
          return {
            current: null,
            page: 0,
            visiblePostID: '',
            pSearch: ''
          }
        },
        mounted(){
          this.$store.dispatch('loadPosts')
        },
        computed: {
          ...mapState([
          'posts'
          ]),
          evenPosts: function(posts){
            return Math.ceil(this.posts.length/6);
          },

          paginatedData() {
            const start = this.page * 6;
            const end = start + 6;
            return this.filteredPosts.slice(start, end);
          },
          filteredPosts() {
            return this.posts.filter((post) => {
              return post.title.match(this.pSearch);
            });
          },
        },
        created(){
          eventEmitter.$on('messageSave', (string) => {
            this.pSearch = string
          })
        }
      }
    </script>

You can wrap title and body in an object 您可以将标题和正文包装在一个对象中

addPost() {
      const post = {
        title: this.createTitle,
        body: this.createBody
      }
      eventEmitter.$emit('postAdd', post)
    }

and then listen as normal 然后照常听

created(){
  eventEmitter.$on('postAdd', (post) => {
    console.log(post)
    // do whatever you want
  })
}

I have not worked on vue js but agreed with @ittus answer. 我没有在Vue js上工作,但同意@ittus答案。 You can make an object consisting of your required data which you want to share across the component and pass it as an event data . 您可以创建一个由所需数据组成的对象 ,该对象要在组件之间共享,并将其作为事件数据传递。

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

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