亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 VueJS 中發出事件?

如何在 VueJS 中發出事件?

慕碼人2483693 2022-07-21 11:03:51
我正在使用 Vuetify 來呈現表格?,F在我將表格分為兩個組件:Table組件和Row組件。如果我不把它分解成兩個獨立的組件,我有一個onclick我可以簡單地在這支筆this.selected = !this.selected!中調用的一個。但是當有 2 個不同的組件時,我怎樣才能發出相同的功能呢?tr看看這個完整的沙箱。這是我的表格組件:-<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>這是我的行組件:-<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>任何幫助將不勝感激。謝謝你。
查看完整描述

1 回答

?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

由于您想監聽 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>

這@click.native意味著它將偵聽組件模板內包裝器 html 元素上的單擊事件


查看完整回答
反對 回復 2022-07-21
  • 1 回答
  • 0 關注
  • 103 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號