假设值:
data(){
	return {
		form: {
			username: '',
			password: '',
			host: 'localhost',
			port: '8080',
			address: ''
		}
	}
}
场景:以上为查询区内容项,点击重置按钮后,需将form中的所有元素恢复成初始值。

** 最最喜欢的用法,即将值重置回原型值。**

Object.assign(this.$data.form, {}, this.$options.data().form);

常用的方法有:
1、将form中的属性挨个赋值,即

this.form.username = ''
this.form.password= ''
this.form.host= 'localhost'
this.form.port = '8080'
this.form.address = ''

2、使用循环的方式,对特殊的属性判断后赋值

Object.keys(this.form).forEach(item => {
	if (item == 'host' || item == 'port') {
		this.form[item] = 'localhost';
		this.form.port = '8080'
	} else {
		this.form[item] = '';
	}
});

3、使用data原型赋值

Object.assign(this.$data.form, {}, this.$options.data().form);
posted on 2022-01-19 21:14  羽丫头不乖  阅读(188)  评论(0编辑  收藏  举报