javascript - Vue js computed result not accurate -


i trying implement vote button vue js, when user click "vote" send axios request server , store data, return json back. same unvote. check if user voted, button should change unvote facebook. far vote , unvote button work correctly. found problems voted features not working. if user voted, after refresh page change "vote", should unvote. if button clicked, in database showing vote deleted. mean should problems of computed. struggle on since not know vue js.

this vue components.

<template>  <a href="#" v-if="isliked" @click.prevent="unlike(comment)">     <span>unvote</span> </a>  <a href="#" v-else @click.prevent="like(comment)">     <span>vote</span> </a> 

<script> export default{     props: ['comment','liked'],     data: function() {         return {             isliked: '',         }     },     mounted() {         axios.get('/comment/'+ this.comment.id +'/check', {})             .then((response) => {                 this.liked = response.data;//here json "true" or "false"              });          this.isliked = this.liked ? true : false;     },     computed: {         islike() {             return this.liked;         },     },     methods: {         like(comment) {             axios.post('/comment/'+ comment.id +'/like')                 .then(response => this.isliked = true)                 .catch(response => console.log(response.data));         },         unlike(comment) {             axios.post('/comment/'+ comment.id +'/unlike')                 .then(response => this.isliked = false)                 .catch(response => console.log(response.data));         },     }  } 

your component instance not have liked data property , should not attempt set prop values (see https://vuejs.org/v2/guide/components.html#one-way-data-flow)

also, attempting set isliked value outside of asynchronous operation not work how think.

just set isliked property...

mounted() {     axios.get('/comment/'+ this.comment.id +'/check', {})         .then((response) => {             this.isliked = response.data; //here json "true" or "false"         }); }, 

your islike computed property never used.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -