【问题标题】:How to remove the duplicated re-initialization code at my vuejs code如何在我的 vuejs 代码中删除重复的重新初始化代码
【发布时间】:2019-02-28 19:08:31
【问题描述】:

在使用 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 = {} 进行重置时,它会删除我的所有属性。但我确实想要。

我怎样才能很好地重构我的代码?

【问题讨论】:

标签: javascript vue.js refactoring


【解决方案1】:

也许

       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;
           }
       }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-11
    • 2021-09-11
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2011-04-12
    • 2021-07-25
    相关资源
    最近更新 更多