pc sound
pc sound

Reputation: 47

[Vue warn]: Error in mounted hook: "ReferenceError: post_id is not defined"

I'm making new Vue 2 component that shows likes count after like button hitted. And get the error:

app.js:5060 [Vue warn]: Error in mounted hook: "ReferenceError: post_id is not defined"

<template> 
        <span>
            <i class="fa fa-heart"></i> {{ likescount }}
        </span>    
 </template> 

<script>
    import { bus } from '../bootstrap';
    import 'vuejs-noty/dist/vuejs-noty.css'
    export default {
        props: ["post_id"],
        
        data: function() {
            return { 
                likescount: 0,
                
            }
        },
        
        created(){
            bus.$on('postliked', (data) => {
               this.updatelikescount(post_id); 
            });
        },
        mounted : function() {
            post_id = {};
            this.updatelikescount(post_id);
        
        },
        
        methods: {
           
            updatelikescount(post_id) {

            axios
                .get('/blog/post/likecount/' + post_id)
                .then(response => {
                    this.likescount = response.data.data[0][0]
                })        
                .catch(response => console.log(response.data));
             },
            
        }
    };
</script>

this is my blade template

<likepostcount 
            :post_id={{ $post->id }}
        ></likepostcount>

When I open the Vue Dev Tools, I see the post_id = 4

Upvotes: 1

Views: 12763

Answers (1)

whoacowboy
whoacowboy

Reputation: 7447

It looks like you just need to add this.

mounted : function() {
  this.post_id = {};
  this.updatelikescount(this.post_id);

},

You code will always set the post_id to an empty object. You probably want to set a default value when you declare the props.

props: {
  post_id: {
    type: Object,
    default: () => {}
  }
},
mounted : function() {
  this.updatelikescount(this.post_id);
},

Upvotes: 1

Related Questions