Vue中變量值變化,但是視圖不更新


今天寫個vue的小demo時,點擊事件中改變了data中變量的值,但是用模板插值到視圖中的數據不更新,代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        li{
            list-style: none;
            width: 200px;
            height: 40px;
            margin: 10px 0;
            background-color: skyblue;
            line-height: 40px;
        }
        #box{
            margin: 200px;
            background-color:pink;
        }
        span{
            float: right;
        }
    </style>
</head>
<body>
    <div id="box">
        <ul>
            <li v-on:click="fun(i)" v-for="(v,i) in arr">{{v.title}}<span>{{v.money}}</span></li>
        </ul>
        {{total}}
        <div v-text="total"></div>
        <p>總價:{{total}}</p>
    </div>
</body>
</html>
<script>
    new Vue({
        el:"#box",
        data:{
            arr:[
                {title:"web Development1",money:300,flag:false},
                {title:"web Development2",money:400,flag:false},
                {title:"web Development3",money:250,flag:false},
                {title:"web Developmen4",money:300,flag:false},
                {title:"web Development5",money:220,flag:false},

            ],
            total:0,
        },
        methods: {
            fun(i){
                console.log(this.arr[i].flag)
                this.arr[i].flag = !this.arr[i].flag;
                if(this.arr[i].flag){
                    this.total =this.total+ this.arr[i].money
                    console.log("====",this.total);
                }else{
                    this.total -= this.arr[i].money
                    console.log(this.total)
                }
            }
        },
    })
</script>

 

 

 

點擊事件中,改變了總價total的值(可以console出結果),但{{total}}中始終為0,

 

 而且使用v-text,變量total也能正常更新,只有插值模板{{}}中的變量不能更新。

經不斷嘗試,加上網上查詢,發現原因居然是Google Chrome瀏覽器自動翻譯的問題,關掉自動翻譯,就OK了。

結論:Google Chrome瀏覽器的自動翻譯,影響了視圖的數據更新(僅會影響模板插值語法{{}}中變量的更新),對v-text無影響。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM