【发布时间】:2021-10-26 21:03:35
【问题描述】:
我有一个正在编辑的表单,我使用 created 方法从 api 调用中预填充该表单信息,一切正常。
但是我想查看表单中的字段,如果它们被编辑,我想将变量“isFormEdited”设置为 true。
我是这样设置的。
watch: {
'form': {
handler: function(v) {
console.log('form changed')
//here i would set the "isFormEdited" variable to be true.
},
deep: true
},
我的问题是控制台日志在页面加载时立即发生,因为表单从空开始,然后创建的函数使用 api 调用填充值。
如何解决这个问题,以便只有在实际手动更改表单时才能将“isFormEdited”值设置为 true?
created() {
const getForm = axios.get(`api/dashboard/form/${this.$route.params.id}/edit`);
const getFormTypes = axios.get('api/dashboard/form/types');
axios.all([getForm, getFormTypes])
.then(
axios.spread((...responses) => {
// console.log(responses[0].data);
this.form = responses[0].data;
if(!this.form.type_id) this.form.type_id = ""; //Set Blank if null
this.form.fields.map((field) => {
// console.log(field);
if(field.options){ field.options = JSON.parse(field.options); }
if(field.mime_types_allowed){ field.mime_types_allowed = JSON.parse(field.mime_types_allowed); }
return field;
});
//get form types
this.types = responses[1].data.data;
})
)
.catch(errors => {
alert("Error");
console.log(errors);
});
提前谢谢你。
【问题讨论】:
标签: javascript vue.js components vuejs3 watch