以父组件内的el-dialog和子组件内的el-form为例,进行父子组件方法调用与动态组件的灵活应用做讲解:
父组件:
<el-dialog
title="提示"
:visible.sync="dialogVisible"
width="30%" >
<components :is="childrenCom" :ref="childrenCom" :nameCom ="childrenCom" @close="close" /> // 动态组件
<span slot="footer" class="dialog-footer">
<el-button @click="changeCom">切换组件</el-button>
<el-button type="primary" @click="click">确 定</el-button>
</span>
</el-dialog>
<script>
components:{
first,
second
},
data(){
return:{
childrenCom:'first',
dialogVisible:true
}
},
methods:{
click(){
this.$refs[`${this.childrenCom}`].submitForm() // 调用 子组件内的方法
},
changeCom(){
this.childrenCom = 'second' //切换组件
},
close(data){
if(!data){
this.dialogVisible = data; // 关闭弹窗
this.$refs[`${this.childrenCom}`].resetForm() // 调用 子组件内的方法
}else{
this.$message('这是一条消息提示');
}
}
}
</script>
子组件:
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item label="活动名称" prop="name">
<el-input v-model="ruleForm.name"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
<el-button @click="resetForm('ruleForm')">重置</el-button>
</el-form-item>
</el-form>
<script>
export default {
props:{