鍍金池/ 問答/HTML/ vue為什么我用var聲明一個變量,然后把this賦值給那個變量變成雙向綁定了

vue為什么我用var聲明一個變量,然后把this賦值給那個變量變成雙向綁定了

shopCountEdit:function(pid , type){
                var productData = this.productData;
                var cartShop = this.cartShop;
                var cartCount = this.cartCount;
                var count = 0;
                for (let i in productData) {
                    if(productData[i].id == pid){
                        if(productData[i].cartcount >= 0){
                            if(!cartShop[pid]){
                                cartShop[pid] = new Object();
                                cartShop[pid].id = pid
                                cartShop[pid].delivertime = 0
                                cartShop[pid].price = productData[i].price
                                cartShop[pid].catid = this.classInfo.id
                                cartShop[pid].count = 0
                            }
                            if(type == 0){
                                var sum = parseInt(productData[i].cartcount) - 1;
                                cartCount = parseInt(cartCount) - 1;
                            }else{
                                var sum = parseInt(productData[i].cartcount) + 1;
                                cartCount = parseInt(cartCount) + 1;
                            }
                            count = sum;
                            productData[i].cartcount = sum;
                            cartShop[pid].count = sum
                        }else{
                            productData[i].cartcount = 0;
                        }
                        break;
                    }
                }
                
                var params = {
                    pid : pid,
                    count : count,
                    token : this.token
                }

                var url = 'index.php?g=Wap&m=Product&a=updateCart&'
                var arr = []
                for (var i in params) {
                    arr.push(i + '=' + params[i])
                }
                url = url + arr.join('&')
                axios.get(url).then((response) => {
                    console.log(response.data.status)
                    if(response.data.status == 1 || response.data.status == 403){
                        this.productData = productData
                        this.cartShop = cartShop;
                        this.cartCount = cartCount;
                        console.log(321)
                    }else if(response.data.status > 402){
                        this.msgContent = response.data.msg;
                        this.msgModel = true;
                    }
                })
            },

1.我想異步判斷是不是超出,如果沒超出才賦值回this里面,但現(xiàn)在他直接沒經(jīng)過異步就雙向綁定了

回答
編輯回答
掛念你

js的數(shù)組是引用類型
引用類型的毛病就是這樣,只是換了個名字,實際上都指向一個地址

2018年3月15日 23:54
編輯回答
萢萢糖

引用的問題,其實改的還是同一個
簡單的話:

var a = {foo: 1}
var b = JSON.parse(JSON.stringify(a));
b.foo = 2;
console.log(a.foo);
console.log(b.foo)

用插件的話
LodashcloneDeep深拷貝

2017年4月11日 05:36