简体   繁体   English

将数据从一个组件传递到另一个组件

[英]Passing data from one component to another

On component1.vue I have在 component1.vue 我有

  export default {
    data () {
      return {
        editItemNumber: null,
        editBreakdownNumber: null
      }
    }

On component2.vue I have a table populated by an array在 component2.vue 我有一个由数组填充的表

On that table is an edit button.在那张桌子上有一个编辑按钮。 Among the item in that table is itemNumber value.该表中的项目是 itemNumber 值。 Which I need to assign from that particular row clicked to the editItemNumber on component1.vue我需要从单击的特定行中将其分配给 component1.vue 上的 editItemNumber

<b-table show-empty bordered striped hover :items="itemTableList" :fields="fields">
  <template slot="actions" scope="row">
<b-btn variant='success' size="sm" @click.stop="edit(row.item,row.index,$event.target)">Edit</b-btn>
</template>
</b-table>

As you see above originally all of this was on one component and I was just calling to an edit function which repopulated the v-models with that rows contents.正如您在上面看到的,最初所有这些都在一个组件上,我只是调用了一个编辑函数,该函数用该行内容重新填充了 v-models。 But now with everything split among components I don't know how to edit this to do what I've been tasked with.但是现在所有组件都拆分了,我不知道如何编辑它来完成我的任务。

I've never used JavaScript, vue or much of anything beyond basic HTML.除了基本的 HTML,我从未使用过 JavaScript、vue 或其他任何东西。 I'm a .NET dev who's been tasked with helping out on some web based work and I'm floundering.我是一名 .NET 开发人员,他的任务是帮助完成一些基于 Web 的工作,而我正在苦苦挣扎。 So any help is appreciated.所以任何帮助表示赞赏。

The preferred way to move data between components is with events.在组件之间移动数据的首选方式是使用事件。

Normally you use props to pass data from a parent component to a child, and events to pass from a child up to a parent.通常,您使用props将数据从父组件传递给子组件,并将事件从子组件传递给父组件。

So the edit method of C2 can be something like所以C2edit方法可以是

edit(item, index, target) {
    const payload = {
        item,
        index,
        target
    };

    this.$emit('edit', payload);
} 

Then you just have to listen to that event in C1 .然后,您只需在C1中收听该事件。 Notice the @edit attribute: that means when the edit event is fired from component-one , run my "edit" method.注意@edit属性:这意味着当从component-one触发edit事件时,运行我的“编辑”方法。

<template>
<div>
   <p>{{ editItemNumber }}</p>
  <component-two @edit="edit" />
</div>

<script>
export default {
data () {
  return {
    editItemNumber: null,
    editBreakdownNumber: null
  };
},

methods: {
    edit(eventPayload) {
        this.editItemNumber = eventPayload.item.editItemNumber
    }
}
</script>

If you both C1 and C2 are children of the same parent P the idea is the same, except C1 can't listen directly to C2 .如果您C1C2都是同一个父母P的孩子,那么想法是相同的,除了C1不能直接听C2 Instead P will listen to the edit event and pass the needed changes down to C1 through its props .相反, P将侦听编辑事件并通过其props将所需的更改传递给C1

The docs on components are really good, pay special attention to the sections on props and custom events.关于组件的文档非常好,特别注意关于道具和自定义事件的部分。 https://v2.vuejs.org/v2/guide/components.html https://v2.vuejs.org/v2/guide/components.html

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

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