javascript - 如何在我的 vuejs 代码中删除重复的重新初始化代码
问题描述
通过 vuejs 制作用户注册表单时,我编写了丑陋的代码。
我想很好地重构,但缺乏想法。由于从遗留代码中添加了 vuejs,我无法使用 vue cli。<script>
所以我嵌入了带有标签的vuejs 。
我想上课的是
newUser : {
"email": '',
"firstName": '',
"lastName": '',
"additionalInfo":{
"phone": '',
"description":''
},
"authority":"TENANT_ADMIN",
"tenantId":{
"entityType": "TENANT",
"id": ''
},
"customerId": {
"entityType": "CUSTOMER",
"id": ''
}
}
这是我的代码。
https://codepen.io/pen/?editors=1111#
要清除现有表单,我确实手动清除了它的属性。
当我尝试使用 重置时this.newUser = {}
,它会删除我的所有属性。但我确实想这样做。
我怎样才能很好地重构我的代码?
解决方案
也许
class ... {
default = {
"email": '',
"firstName": '',
"lastName": '',
"additionalInfo":{
"phone": '',
"description":''
},
"authority":"TENANT_ADMIN",
"tenantId":{
"entityType": "TENANT",
"id": ''
},
"customerId": {
"entityType": "CUSTOMER",
"id": ''
}
}
newUser = {}
constructor() {
this.init();
}
init() {
this.newUser = this.default;
}
clear() {
this.newUser = this.default;
}
}
推荐阅读
- java - 在 JAVA 中使用 XSLT 转换 XML
- python - tkinter 方法 .bell() 没有声音
- django - 如何修复 django 频道消费者停止处理发送到组的消息
- python-3.x - 如何修复 QtWidgets.QMessageBox 中的“TypeError ......”以在 PyQT5 中弹出消息
- python - 如何在 Pandas 中将单列数据框转换为单行数据框?
- ios - 如何从自定义 UITableview 单元中获取 tableView 实例?
- java - 如何在 Java 11 中生成代码,但针对 Java 8 及更高版本?
- java - 如何将节点动态添加到 Oracle ADF 中的 RichTree?
- python - 如何加快搜索算法
- google-cloud-platform - Google Cloud DataProc 是否提供具有默认 JVM 和 YARN 设置的网页?