vue中this.$emit的用法 使用this.$refs的方法


vue中this.$emit的用法

用於當子組件需要調用父組件的方法的場景下使用。

this.$emit('事件',參數)

與之相對的當父組件需要調用子組件時則使用this.$refs的方法

this.$refs.子組件的ref.子組件的方法

實例

為了能清晰的了解具體用法,准備了一個父子組件互調方法的例子。

父組件

父組件調用子組件需要導入子組件的路徑並添加組件之后添加子組件標簽即可。

<template>
  <div class="app-container">
    <div class="the-container">
      <el-table
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          type="index"
        />
        <el-table-column
          prop="date"
          label="日期"
          show-overflow-tooltip
        />
        <el-table-column
          prop="name"
          label="姓名"
          show-overflow-tooltip
        />
        <el-table-column
          prop="province"
          label="省份"
          show-overflow-tooltip
        />
        <el-table-column
          prop="city"
          label="市區"
          show-overflow-tooltip
        />
        <el-table-column
          prop="address"
          label="地址"
          show-overflow-tooltip
        />
        <el-table-column
          prop="zip"
          label="郵編"
          show-overflow-tooltip
        />
        <el-table-column
          label="操作"
          width="120"
        >
          <template slot-scope="{row}">
            <el-button
              type="text"
              @click="alter(row)"
            >
              修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <Amend ref="amend" @submit="refresh"></Amend>
  </div>
</template>

<script>
// 引入子組件
import Amend from './amend'
export default {
  components: {
    // 子組件名稱
    Amend
  },
  data() {
    return {
      // 表格數據
      tableData: [{
        date: '2016-05-03',
        name: '小虎',
        province: '上海',
        city: '普陀區',
        address: '上海市普陀區金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-02',
        name: '小張',
        province: '上海',
        city: '普陀區',
        address: '上海市普陀區金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-04',
        name: '小明',
        province: '上海',
        city: '普陀區',
        address: '上海市普陀區金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-01',
        name: '小紅',
        province: '上海',
        city: '普陀區',
        address: '上海市普陀區金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-08',
        name: '小李',
        province: '上海',
        city: '普陀區',
        address: '上海市普陀區金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-06',
        name: '小劉',
        province: '上海',
        city: '普陀區',
        address: '上海市普陀區金沙江路 1518 弄',
        zip: 200333
      }]
    }
  },
  methods: {
    alter(row) {
      // 調用子組件方法並傳參,row為該行數據
      this.$refs.amend.show(row)
      console.log(row)
    },
    refresh(data) {
      // 子組件調用父組件的方法並傳參
      console.log(data)
    }
  }
}
</script>

<style lang="scss" scoped>
  .app-container {
    height: 100%;
    background-color: #f1f1f1;
  }
  .the-container{
    padding: 20px;
    height: 100%;
    background-color: #fff;
  }
</style>

子組件

子組件在調用父組件是需要在父組件中添加事件來觸發父組件的方法。

<template>
  <el-dialog
    title="提示"
    :visible.sync="dialogVisible"
    width="50%"
    :before-close="handleClose"
    append-to-body
  >
    <el-form ref="ruleForm" :model="ruleForm" label-width="100px">
      <el-form-item label="日期" prop="date">
        <el-input v-model="ruleForm.date" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-input v-model.number="ruleForm.province" />
      </el-form-item>
      <el-form-item label="市區" prop="city">
        <el-input v-model.number="ruleForm.city" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model.number="ruleForm.address" />
      </el-form-item>
      <el-form-item label="郵編" prop="zip">
        <el-input v-model.number="ruleForm.zip" />
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button type="primary" @click="submitForm">確定</el-button>
      <el-button @click="handleClose">取消</el-button>
    </span>
  </el-dialog>

</template>

<script>
export default {
  name: 'Amend',
  data() {
    return {
      // 是否顯示對話框,true為顯示,false為不顯示
      dialogVisible: false,
      // 表單數據
      ruleForm: {
        date: '',
        name: '',
        province: '',
        city: '',
        address: '',
        zip: 1
      },
      // 返回值數據
      formList: {}
    }
  },
  methods: {
    show(row) {
      // 將該行數據賦給表單
      this.ruleForm = row
      console.log(this.ruleForm)
      this.dialogVisible = true
    },
    handleClose() {
      this.dialogVisible = false
    },
    submitForm() {
      this.formList = this.ruleForm
      this.dialogVisible = false
      // 子組件調用父組件的方法並傳參
      this.$emit('submit', this.formList)
    }
  }
}
</script>

<style scoped>

</style>

以上為小編總結的,this.$emit的使用方法,還有this.$refs的使用方法,不理解的小伙伴可以多看幾遍。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM