S
S
Sasha2019-01-03 19:36:48
Laravel
Sasha, 2019-01-03 19:36:48

How to resolve Vue component error Error in mounted hook: "ReferenceError: post_id is not defined"?

Hey! I am writing a Vue component to display the number of likes (Laravel 5.5 + Vue v.2.5.16):
an error occurs

Error in mounted hook: "ReferenceError: post_id is not defined"

Component code:
<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() {
            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>

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

When opening VueDevTools the component and property are visible, the post_id value is correctly populated.
But the error does not allow displaying the number of likes, they remain unchanged = 0.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sasha, 2019-01-03
@lipasite

Decided like this:

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question