计算属性不会触发

时间:2017-03-02 16:53:32

标签: javascript vuejs2

我有一个非常基本的例子。当我点击单选按钮时,{{categoryFullName}}应该更新:

<div id="create_category" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-body">
                <div class="col-xs-12" id="modal">

                    <div class="row">
                        <div class="col-xs-12">
                            <div class=" form-group border-grey-700">
                                <p class="full-width border-lg p-20 text-bold text-size-large text-center text-uppercase">
                                    {{ categoryFullName }} </p>
                                <hr/>
                            </div>
                        </div>
                    </div>


                    <div class="row">

                        <div class="col-md-4 col-md-offset-2">

                            <div class=" form-group">

                                <label for="isTeam" class="text-bold">Team</label>
                                <br/>

                                <div>
                                    <input type="radio" name="isTeam" id='yes' value="1" v-model="isTeam"/>
                                    <label for="yes"> Si</label>

                                    &nbsp;&nbsp;&nbsp;
                                    <input type="radio" name="isTeam" id='no' value="0" v-model="isTeam"/>
                                    <label for="no">No</label>
                                </div>
                            </div>

                        </div>

                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<script>
    let team = 'team';
    let single = 'single';
</script>

我的Vue脚本:

new Vue({
    el: '#create_category',
    data: {
        isTeam: 0

    },
    computed: {
        categoryFullName: () => {
            console.log("computed");
            return this.isTeam == 1 ? "team" : "single";
        }
    },
});

现在Computed属性被触发一次,但是当单选按钮改变时它不会触发......

我缺少什么?我让它在VueJS 1中工作......

小提琴:https://jsfiddle.net/xoco70/x3sLus6n/21/

1 个答案:

答案 0 :(得分:0)

在计算机中引用on(), off(), trigger()时,请勿使用胖箭头。通常,在Vue中,避免对方法,计算等使用胖箭头函数

this

胖箭头函数以词法方式绑定computed: { categoryFullName: function() { console.log("computed"); return this.isTeam == 1 ? "team" : "single"; } }, 。由于Vue接受函数并将其附加到Vue对象,如果使用胖箭头来定义计算或方法,this最终会引用错误的对象。

相关问题