3

我的 vuex 商店有问题。我正在使用 vue2 和 vuex2。

当我调度某个动作时,它会改变我商店中 2 个元素的状态。这是我状态的一部分,它会影响一些虚拟数据来解释:

  {
     "clients":[
        {
           "user":{
              "id":7,
              "name":"Luis",
              "lastName":"Cervantes",
              "lastName2":"",
              "isCompany":0,
              "razonSocial":null,
              "cif":"",
              "documentType":"NIE",
              "document":"asdasd",
              "phone":"232344455",
              "phone2":"",
              "email":"louis.cervantes@gmail.com",
              "active":0,
              "setPassword":0,
              "setEmail":1,
              "active_token":null,
              "last_login":"2016-09-08 15:57:09",
              "created_at":"2016-09-08 15:57:09",
              "updated_at":"2016-09-12 22:05:36"
           },
           "addresses":[
              {
                 "id":5,
                 "user_id":7,
                 "province":"Almería",
                 "city":"Almeria",
                 "postCode":"23000",
                 "streetType":"Calle",
                 "address":"Riudoms",
                 "number":"44",
                 "other":"",
                 "address_id":2,
                 "created_at":"2016-09-12 22:02:13",
                 "updated_at":"2016-09-12 22:05:58",
                 "default":1
              },
              {
                 "id":3,
                 "user_id":7,
                 "province":"Araba/Álava",
                 "city":"Alava",
                 "postCode":"23232",
                 "streetType":"Camino",
                 "address":"nueva",
                 "number":"2323",
                 "other":"",
                 "address_id":1,
                 "created_at":"2016-09-08 15:57:09",
                 "updated_at":"2016-09-13 17:28:30",
                 "default":0
              }
           ]
        },
        {
           "user":{
              "id":8,
              "name":"Richard",
              "lastName":"Czyrny",
              "lastName2":"",
              "isCompany":0,
              "razonSocial":null,
              "cif":"",
              "documentType":"NIE",
              "document":"x84445556V",
              "phone":"937444566",
              "phone2":"",
              "email":"omas@gmail.com",
              "active":0,
              "setPassword":0,
              "setEmail":1,
              "active_token":null,
              "last_login":"2016-09-08 15:58:07",
              "created_at":"2016-09-08 15:58:07",
              "updated_at":"2016-09-12 20:11:28"
           },
           "addresses":[
              {
                 "id":4,
                 "user_id":8,
                 "province":"Tarragona",
                 "city":"Salou",
                 "postCode":"43840",
                 "streetType":"Carretera",
                 "address":"Costa",
                 "number":"35",
                 "other":"1 2",
                 "address_id":3,
                 "created_at":"2016-09-12 21:30:46",
                 "updated_at":"2016-09-13 16:30:10",
                 "default":1
              }
           ]
        }
     ],
     "client":{
           "user":{
              "id":7,
              "name":"Luis",
              "lastName":"Cervantes",
              "lastName2":"",
              "isCompany":0,
              "razonSocial":null,
              "cif":"",
              "documentType":"NIE",
              "document":"asdasd",
              "phone":"232344455",
              "phone2":"",
              "email":"louis.cervantes@gmail.com",
              "active":0,
              "setPassword":0,
              "setEmail":1,
              "active_token":null,
              "last_login":"2016-09-08 15:57:09",
              "created_at":"2016-09-08 15:57:09",
              "updated_at":"2016-09-12 22:05:36"
           },
           "addresses":[
              {
                 "id":5,
                 "user_id":7,
                 "province":"Almería",
                 "city":"Almeria",
                 "postCode":"23000",
                 "streetType":"Calle",
                 "address":"Riudoms",
                 "number":"44",
                 "other":"",
                 "address_id":2,
                 "created_at":"2016-09-12 22:02:13",
                 "updated_at":"2016-09-12 22:05:58",
                 "default":1
              },
              {
                 "id":3,
                 "user_id":7,
                 "province":"Araba/Álava",
                 "city":"Alava",
                 "postCode":"23232",
                 "streetType":"Camino",
                 "address":"nueva",
                 "number":"2323",
                 "other":"",
                 "address_id":1,
                 "created_at":"2016-09-08 15:57:09",
                 "updated_at":"2016-09-13 17:28:30",
                 "default":0
              }
           ]
        },
  }

客户阵列。

以及包含当前选定客户端的客户端对象。我有分别更改客户端用户对象和地址对象的操作

行动

SET_CLIENT_PROFILE_USER: ({commit, state}, user) => {
  commit('CLIENT_PROFILE_USER', user)
},

SET_CLIENT_PROFILE_ADDRESSES: ({commit, state}, addresses) => {
  commit('CLIENT_PROFILE_ADDRESSES', addresses)
}

突变

'CLIENT_PROFILE_USER': (state, user) => {
   state.client.user = user
 },
'CLIENT_PROFILE_ADDRESSES': (state, addresses) => {
   state.client.addresses = addresses
 }

这是我在一个组件中使用的一些计算属性。

clientsList(){
  return this.$store.getters.getClients
},
client(){
  return this.clientsList[this.client_index]
},
addresses(){
  if(this.client){
    return this.client.addresses
  }else{
    return ''
  }
},
address(){
 if(this.addresses){
    return this.client.addresses[this.address_index]
  }else{
    return ''
  }
},

这是我使用的一些观察者,他们调度动作

watch: {
  'client_index': function (val, oldVal) {
    var c = this.client;
    // console.log(c)
    this.$store.dispatch('SET_CLIENT_PROFILE', c);
    this.address_index = '';
  },
  'address_index': function(val, oldVal){
    if(this.val != ''){
      var a = this.address;
      // console.log(a)
      this.$store.dispatch('SET_CLIENT_PROFILE_ADDRESSES', a);
    }
  }
 },

并且当SET_CLIENT_PROFILE_ADDRESSES被调度时,会更改client.addresses和与该地址对应的客户对象中的地址。它应该只更改client.addresses部分。

我找不到它这样做的原因。

我只想更改client.adresses

4

1 回答 1

1

我发现对象和数组通过引用传递给同一个对象,而不是通过值传递是 JavaScript 默认行为。

我使用了 Lodash 的 cloneDeep 函数。

于 2016-09-14T16:32:41.227 回答