vue中v-bind=“$attrs“、v-on=“$listeners“

一、背景

    在封裝組件時(shí)候,經(jīng)常要傳遞參數(shù),調(diào)用事件。我們希望把主導(dǎo)權(quán)放在業(yè)務(wù)組件內(nèi),這是引入我們今天的主題: `v-bind="$attrs"` `v-on="$listeners"`

    使用 `v-bind="$attrs" `屬性,`vm.$attrs `是一個(gè)屬性,其包含了**父作用域中不作為 prop 被識(shí)別 (且獲取) 的特性綁定 (class 和 style 除外)**。這些**未識(shí)別的屬性**可以通過` v-bind="$attrs" `傳入內(nèi)部組件。**未識(shí)別的事件**可通過`v-on="$listeners"`傳入。

父組件

// 業(yè)務(wù)代碼
<template>
  <div class="container">
    <div>
      <el-button @click="showDialog" type="primary" style="margin:50px;">show-dialog</el-button>
      <test-dialog
        top="8vh"
        width="85%"
        :title="'dialog-title'"
        :visible.sync="dialogVisible"
        :footer-visible="true"
        @refuse="handleClose"
        @confirm="handleConfirm"
        @closeBtn="handleClose"
      >
        <template #content>
          <div>
            content
          </div>
        </template>
      </test-dialog>
    </div>
  </div>
</template>
 
<script>
import TestDialog from "./components/dialog.vue";
export default {
  components: {
    TestDialog
  },
  data() {
    return {
      dialogVisible: false,
    };
  },
  methods: {
    showDialog(){
      console.log('hide')
      this.dialogVisible = true
    },
    handleClose() {
      console.log('close')
      this.dialogVisible = false;
    },
    handleConfirm() {
      console.log('success')
    },
  }
};
</script>

子組件

// 組件代碼
<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      ref="mDialog"
      :visible="$attrs.visible || instanceVisible"
      :width="'500px'"
      :show-close="false"
      v-on="$listeners"
    >
      <div slot="title" class="dialog--title">
        <span>{{ $attrs.title || "提示" }}</span>
        <span class="dialog--title__close" @click="close">
          <i class="el-icon-close"></i>
        </span>
      </div>
      <div class="content">
        <slot name="content" />
      </div>
      <div slot="footer">
        <div>
          <el-button refuse="true" plain @click="refuse">{{
            $attrs["refuse-text"] || "取消"
          }}</el-button>
          <el-button type="primary" @click="confirm">{{
            $attrs["confirm-text"] || "確定"
          }}</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { throttle } from "lodash";
 
export default {
  name: "Dialog",
  inheritAttrs: false,
  data() {
    return {
      instanceVisible: false,
    }
  },
  methods: {
    close() {
      this.$listeners.closeBtn();
    },
    refuse: throttle(
      function func() {
        this.$listeners.refuse();
      },
      1500
    ),
    confirm: throttle(
      function func() {
        this.$listeners.confirm();
      },
      1500
    )
  }
}
</script>
<style lang="less">
.dialog--title {
  font-size: 18px;
  line-height: 24px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #e5e5e5;
  margin-bottom: 20px;
  .dialog--title__close:hover {
    cursor: pointer;
  }
}
</style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容