Vue.js挂载函数运行得太早

时间:2017-04-06 11:58:01

标签: javascript vue.js

基本上我要做的是运行一个函数来添加不同的变量并设置一个新的变量非常简单:

addup: function () {
                        this.homeScore1 = this.m1g1h + this.m1g2h + this.m1g3h + this.m1g4h + this.m2g1h + this.m2g2h + this.m2g3h + this.m2g4h;
                        this.awayScore1 = this.m1g1a + this.m1g2a + this.m1g3a + this.m1g4a + this.m2g1a + this.m2g2a + this.m2g3a + this.m2g4a;
                        this.homeScore2 = this.m3g1h + this.m3g2h + this.m3g3h + this.m3g4h + this.m4g1h + this.m4g2h + this.m4g3h + this.m4g4h;
                        this.awayScore2 = this.m3g1a + this.m3g2a + this.m3g3a + this.m3g4a + this.m4g1a + this.m4g2a + this.m4g3a + this.m4g4a;
                        this.hdoubles = this.m5g1h + this.m5g2h + this.m5g3h + this.m5g4h;
                        this.adoubles = this.m5g1a + this.m5g2a + this.m5g3a + this.m5g4a;
                        alert(this.m1g1h);
                        //total handicap is set here
                        this.hhandicap = this.singlesHHandicap +  this.doublesHHandicap;
                        this.ahandicap = this.singlesAHandicap +  this.doublesAHandicap;

                        //total score is calculated here
                        this.homescore = this.homeScore1 + this.homeScore2 + this.hdoubles + this.hhandicap;
                        this.awayscore = this.awayScore1 + this.awayScore2 + this.adoubles +this.ahandicap;
                    },

例如,this.m1g1h的值在名为this.updatesSinglesScores()的函数中设置;它在页面创建时运行,它调用我的数据库并将返回的值分配给某些vue变量:

created: function () {
                    this.updatesSinglesScores();
                    this.updateDoublesScores();
                },

之后我在挂载时调用addup函数:

mounted: function () {
                    this.addup();
                }

所以我遇到的问题是变量this.homeScore1,例如,在页面的html中显示的变量,它仍然为0.在进一步检查addup函数中的警报后,我学会了this.m1g1h保持为0,即使它应该是另一个值。此外,如果我使用按钮运行addup功能一切正常。那么有人可以向我解释为什么this.m1g1h仍为0?还有为什么从按钮调用时addup功能有效?

0 个答案:

没有答案