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>