父组件代码
<!-- 父组件 -->
<template>
<div class="abutton">
<el-button type="primary" @click="clicked">点击</el-button>
<mode-one ref="firstDom" class="mode"></mode-one>
</div>
</template>
<script>
import modeOne from './modeOne.vue'
export default {
components: { modeOne },
data(){
return{}
},
methods:{
clicked(){
// 父组件要传到子组件的数据
let form = {
name: '中百超市大促销',
region: '中百超市',
date1: '2021-07-17',
delivery: true,
type: ['美食/餐厅线上活动','地推活动','线下主题活动','单纯品牌曝光'],
desc: '线上品牌商赞助'
}
// setData为父组件向子组件传值自定义的方法名
this.$refs.firstDom.setData(form)
}
}
}
</script>
<style lang="less" scoped>
.mode{
width: 500px;
margin: auto;
}
</style>
子组件代码
<!-- 子组件 -->
<template>
<div>
<el-form ref="form" :model="form" label-width="80px">
<el-form-item label="活动名称">
<el-input v-model="form.name"></el-input>
</el-form-item>
<el-form-item label="活动区域">
<el-input v-model="form.region"></el-input>
</el-form-item>
<el-form-item label="活动时间">
<el-date-picker type="date" value-format="yyyy-MM-dd" placeholder="选择日期" v-model="form.date1" style="width: 100%;"></el-date-picker>
</el-form-item>
<el-form-item label="即时配送">
<el-switch v-model="form.delivery"></el-switch>
</el-form-item>
<el-form-item label="活动性质">
<el-checkbox-group v-model="form.type">
<el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
<el-checkbox label="地推活动" name="type"></el-checkbox>
<el-checkbox label="线下主题活动" name="type"></el-checkbox>
<el-checkbox label="单纯品牌曝光" name="type"></el-checkbox>
</el-checkbox-group>
</el-form-item>
<el-form-item label="活动形式">
<el-input type="textarea" v-model="form.desc"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">立即创建</el-button>
<el-button>取消</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
data(){
return{
form: {
name: '',
region: '',
date1: '',
delivery: false,
type: [],
desc: ''
}
}
},
methods:{
// 子组件接收父组件传值对应的同名方法
setData(data){
this.form = data //父组件传过来的值
},
onSubmit(){
console.log(this.form)
}
}
}
</script>