简体   繁体   中英

Computed property is not triggering

I have a very basic example. When I click on the radio button, {{ categoryFullName }} should be updated:

<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>

My Vue Script:

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

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

Now Computed property get triggered once, but it doesn't trigger when radio button change...

What am I missing??? I had it working in VueJS 1...

The fiddle: https://jsfiddle.net/xoco70/x3sLus6n/21/

Don't use a fat arrow when you reference this inside your computed. In general, in Vue, avoid using fat arrow functions for methods, computed, etc.

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

Fat arrow functions bind this lexically. Since Vue takes the function and attaches it to the Vue object, this ends up referring to the wrong object if you use a fat arrow to define your computed or method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM