javascript - 如何在 vuejs2 中将道具从子级重置为父级
问题描述
我制作了一个表单模板(form.vue
),其中包含保存功能以避免重复它和 HTML 的某些部分,我还想将它发送到那里并重新启动父表单的道具(authors_form.vue
)
这个模板是孩子:
<template>
<v-flex xs12 sm12 lg6>
<v-card>
<v-toolbar color="pink">
<v-card-title primary-title>{{ source.title }}</v-card-title>
</v-toolbar>
<v-form v-model="valid" >
<slot name='content-form'></slot>
</v-form>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="green darken-1" flat="flat" @click="save()" :disabled="!valid">Save</v-btn>
<v-btn color="green darken-1" flat="flat" @click="$router.push(source.return)">Cancel</v-btn>
</v-card-actions>
</v-card>
<v-dialog v-model="dialog" max-width="290">
<v-card>
<v-card-title class="headline">{{ msm }}</v-card-title>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="green darken-1" flat="flat" @click="$router.push(source.return)">return to list</v-btn>
<v-btn color="green darken-1" flat="flat" @click.native="resetForm">continue here</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-flex>
</template>
<script>
export default {
props:{
source:Object,
form:Object
},
data: () => ({
valid: true,
dialog:false,
msm:'',
error:false
}),
methods:{
save(){
let vm=this
axios[this.source.method](this.source.create,this.form)
.then(function(response) {
Vue.set(vm.$data,'msm',response.data)
})
.catch(function(error) {
Vue.set(vm.$data, 'msm', error.response)
Vue.set(vm.$data,'error',true)
})
this.dialog=true
},
resetForm(){
/*
reset props
*/
this.dialog=false
}
}
}
</script>
发送的道具是一个对象,因为我认为它使模板更可重用,起初我使用 $ emit 但这意味着我必须在每个表单中定义重置方法(authors_form.vue
, books_form.vue
,editorial_form .vue
等。 ..)。
此模板是以下表单之一(父级):
<template>
<v-layout justify-center align-center >
<form-component :source='source' :form='form'>
<div slot='content-form'>
<v-text-field v-model='form.name' :rules="nameRules" ></v-text-field>
</div>
</form-component>
</v-layout>
</template>
<script>
import formComponent from "../utilities/form.vue"
export default {
data: () => ({
source:{
title:'Add Author',
create:'admin/Authors',
method:'post',
return:'/Authors'
},
form:{
name:''
},
nameRules:[
v=> /^[A-Za-z\s]*$/gi.test(v) || 'sorry, the only letters ',
v=> !!v || 'sorry, the name es rquired'
],
}),
beforeMount(){
if(this.$route.meta.mode === 'edit') {
this.source.title = 'Edit author'
this.source.create = 'admin/Authors/' + this.$route.params.id
this.source.method = 'put'
this.form.name=this.$route.params.name
}
},
components:{
formComponent
}
}
</script>
解决方案
推荐阅读
- javascript - 在 Google 地理图表中向下钻取
- javascript - 哈希已更新,但浏览器仍使用 gulp-rev 加载旧缓存
- java - 为什么当 for 循环进行时我的字符计数器没有增加?
- javascript - 如何在 vuejs 中生成 pdf(已更新)
- c++ - 模板重载解析:多个模板匹配时会发生什么?
- reactjs - reactjs中如何将数组从父组件传递给子组件
- php - 使用 jquery 添加到购物车按钮
- javascript - Javascript从所选框中获取错误值
- aws-sdk - AwsCrypto 线程安全吗?我们可以一次创建 AWSCrypto 的对象并将其用于所有请求吗?
- javascript - 通过单击提交按钮显示输入