angry kiwi
angry kiwi

Reputation: 11445

laravel vue how to use plugin

I'm trying the vue cook plugin in laravel vue app, this is how I do it

<template>
    <div class="tranbg" v-if="first">
        <div>hello</div>
        <button @click="turnOff">ok</button>        
    </div>
</template>

<script>


export default {
    data(){
        return {
            first: true
        }
    },
    computed(){
        if (this.$cookie.get('firstvisit') == 'visited') {
            this.first = false
        }
    },
    methods:{

        turnOff(){
            this.first = false
            this.$cookie.set('firstvisit', 'visited', 30000); // 30,000 days
        }
    }   
}
</script>

my app.js

import VueCookies from '../../../node_modules/vue-cookies'
Vue.use(VueCookies)

const app = new Vue({
    el: '#app',
});

Got the error app.js?v=226:103199 Uncaught TypeError: Cannot read property 'set' of undefined when I click the button

Upvotes: 0

Views: 1534

Answers (1)

padarom
padarom

Reputation: 3648

The Vue.js documentation tells you to register plugins before initializing your Vue instance.

At some point in your application you create a new Vue(...) instance. You need to call Vue.use(VueCookies) before that, instead of in your component's file. Since Vue might already be instantiated by the time your component is imported the $cookie property isn't yet set.

Upvotes: 1

Related Questions