Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue 2 props undefined

Please help! I have 'undefined' props in child template.

Main js:

// some code before
window.Vue = require('vue');
import EventBus from '../components/event-bus';
// some code after

Vue.component('calendar-select', require('../components/admin/calendar_select.vue'));

const app = new Vue({
    el: '#app',
    data: function() {
        return {
            isActiveCalendar: true
        }
    },
    methods: {
        toggleCalendar() {
            this.isActiveCalendar = !this.isActiveCalendar;
        }
    },
    mounted() {
        EventBus.$on('toggleCalendar', this.toggleCalendar);
    }
});

After this I created template like this:

<template>
        <div class="custom-select" v-bind:class="{ active: isActiveCalendar}" >
            <div class="box flex" v-on:click="toggleCalendar" >
                <span>Calendar</span>
                <img src="/assets/img/admin/arrow-down.png" alt="#">
            </div>
        </div>
    </div>
</template>

<script>
import EventBus from '../event-bus';
export default 
{
// ------- start
    props: ['isActiveCalendar'],
    data: function() {
        return {
        }
    },
    methods: {
        toggleCalendar: function(event) {
            console.log(this.isActiveCalendar)
            EventBus.$emit('toggleCalendar');
        }
    }
// ------- end
}
</script>

When I do console.log on this.isActiveCalendar, the variable is undefined and in Vue plugin for Chrome is same thing.

Please, tell me, what mistake I am doing!

Thanks!

like image 543
KoGoro Avatar asked Aug 18 '17 15:08

KoGoro


1 Answers

As stated in the documentation for passing props.

HTML attributes are case-insensitive, so when using non-string templates, camelCased prop names need to use their kebab-case (hyphen-delimited) equivalents:

In this example you would need to use

<calendar-select v-bind:is-active-calendar="isActiveCalendar"></calendar-sele‌​ct>

so that it would pass the value of the variable into the prop.

like image 75
Justin MacArthur Avatar answered Sep 17 '22 14:09

Justin MacArthur