简体   繁体   English

如何在 VueJS 中发出事件?

[英]How to emit an event in VueJS?

I am using Vuetify to render a table.我正在使用 Vuetify 来呈现表格。 Now i have divided the table in two components:- Table component and the Row component.现在我将表格分为两个组件:- Table组件和Row组件。 If i don't break it down into two separate component, i have an onclick that i can simply call this.selected =.this.selected!如果我不把它分解成两个单独的组件,我有一个onclick ,我可以简单地调用this.selected =.this.selected! on a tr in this pen .在这支上的一个tr上。 But how can i emit the same function when there's 2 different components?但是当有 2 个不同的组件时,我怎么能发出相同的 function 呢?

Check out this complete sandbox .看看这个完整的沙箱

This is my Table component:-这是我的表格组件:-

<template>
  <div class="hello">
    <v-container>
      <v-layout>
        <v-data-table
          v-model="selected"
          :headers="getHeaders"
          :items="getTableItems"
          item-key="name"
          class="elevation-1"
        >
          <template v-slot:headers="props">
            <tr>
              <th>
                <v-checkbox
                  :input-value="props.all"
                  :indeterminate="props.indeterminate"
                  primary
                  hide-details
                  @click.stop="toggleAll"
                ></v-checkbox>
              </th>
              <th v-for="header in props.headers" :key="header.text">
                <v-icon small>arrow_upward</v-icon>
                {{ header.text }}
              </th>
            </tr>
          </template>
          <template v-slot:items="props">
            <Row
              :active="props.active"
              :selected="props.selected"
              :name="props.item.name"
              :calories="props.item.calories"
              :fat="props.item.fat"
            />
          </template>
        </v-data-table>
      </v-layout>
    </v-container>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Row from "./Row";
export default {
  name: "Table",
  components: {
    Row
  },
  data() {
    return {
      selected: []
    };
  },
  computed: {
    ...mapGetters({
      getHeaders: "getHeaders",
      getTableItems: "getTableItems"
    })
  },
  methods: {
    toggleAll() {
      if (this.selected.length) this.selected = [];
      else this.selected = this.getTableItems.slice();
    }
  }
};
</script>

This is my row component:-这是我的行组件:-

<template>
  <tr :active="active">
    <td>
      <v-checkbox :input-value="selected" primary hide-details></v-checkbox>
    </td>
    <td>{{name}}</td>
    <td>{{calories}}</td>
    <td>{{fat}}</td>
  </tr>
</template>

<script>
export default {
  props: {
    active: Boolean,
    selected: Boolean,
    name: String,
    calories: Number,
    fat: Number
  }
};
</script>

And this is my Vuex store:-这是我的 Vuex 商店:-

state: {
    headers: [
      { text: "Dessert (100g serving)", align: "left", value: "name" },
      { text: "Calories", value: "calories" },
      { text: "Fat (g)", value: "fat" }
    ],
    tableItems: [
      { name: "Frozen Yogurt", calories: 159, fat: 6.0 },
      { name: "Ice cream sandwich", calories: 237, fat: 9.0 }
    ]
  },
  getters: {
    getHeaders: state => state.headers,
    getTableItems: state => state.tableItems
  },

Any help will be appreciated.任何帮助将不胜感激。 Thank you.谢谢你。

Since you want to listen to the click event of the TR element, you don't need to emit an event or anything, you can do something like:由于您想监听 TR 元素的点击事件,因此不需要发出事件或任何东西,您可以执行以下操作:

<template v-slot:items="props">
  <Row
    :active="props.active"
    :selected="props.selected"
    :name="props.item.name"
    :calories="props.item.calories"
    :fat="props.item.fat"
    @click.native="props.selected = !props.selected"
  />
</template>

that @click.native means that it will listen for the click event on the wrapper html element inside the template of the component @click.native意味着它将侦听组件模板内包装器 html 元素上的单击事件

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

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