[Vue警告]:安装的挂钩中出现错误:“ ReferenceError:未定义post_id”

时间:2019-01-03 16:08:27

标签: laravel vuejs2

我正在制作新的Vue 2组件,该组件在按了“赞”按钮后显示喜欢计数。并得到错误:

app.js:5060 [Vue警告]:安装的挂钩中出现错误:“ ReferenceError:未定义post_id”

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

这是我的刀片模板

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

打开Vue开发工具时,我看到post_id = 4

1 个答案:

答案 0 :(得分:1)

看起来您只需要添加它即可。

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

},

您的代码将始终将post_id设置为空对象。声明道具时,您可能想设置一个默认值。

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