子组件CateAdd.vue
<el-button type="primary" @click="onSubmit">立即创建</el-button>
onSubmit () {
this.$refs.form.validate((valid) => {
if (valid) {
console.log(this.cate)
this.$emit('addCate', this.cate)
} else {
this.$message({
type: 'error',
message: '表单验证不通过'
})
}
})
}
import { getCateLists, addCate } from '../../api'
父组件 CateLists index.vue
<cate-add :cates="allCates" @addCate ="fn"/>
methods中
fn (cate) {
addCate(cate).then(res => {
if (res.data.code === 200) {
this.$message({
type: 'success',
duration: 1200,
onClose: () => {
this.$router.go(0)
}
})
}
})
}