[Vuejs]-Avoid mutating a prop directly

3👍

Initialise a data attribute from your prop, and manipulate that.

export default {
    data() {
        return {
            numberOfLikes: this.likescount
        }
    },

    props: [
        'isliked',
        'path',
        'type',
        'likescount'
    ],

    methods:{
        like() {
            axios.post(`/${this.type}/${this.path}/like/`);

            // ...

            this.numberOfLikes++;
        },

        unlike() {
            axios.post(`/${this.type}/${this.path}/unlike/`);

            // ...

            this.numberOfLikes--;
        },
    }
}

0👍

Just as the warning says you shall not mutate props, remove
this.likescount ++;
this.likescount –;

and this will remove the warning … your props should be pure

👤ehab

Leave a comment