当调度操作时,Vuex会更改状态中的2个元素

时间:2016-09-14 15:52:30

标签: javascript vue.js vuex

我的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.addreses 客户端对象中的地址对应于那个地址。 它应该只更改 client.addresses 部分。

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

我只想更改 client.adresses

1 个答案:

答案 0 :(得分:1)

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

我使用了Lodash的cloneDeep函数。