【问题标题】:laravel vue how to use pluginlaravel vue 插件使用方法
【发布时间】:2018-11-15 10:56:38
【问题描述】:

我正在尝试 laravel vue 应用中的 vue cook 插件,我就是这样做的

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

我的 app.js

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

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

收到错误 app.js?v=226:103199 Uncaught TypeError: Cannot read property 'set' of undefined 当我单击按钮时

【问题讨论】:

    标签: laravel vue.js


    【解决方案1】:

    Vue.js documentation 告诉您在初始化 Vue 实例之前注册插件。

    在您的应用程序的某个时刻,您会创建一个new Vue(...) 实例。 您需要在此之前调用Vue.use(VueCookies),而不是在组件的文件中。由于在导入组件时 Vue 可能已经实例化,因此尚未设置 $cookie 属性。

    【讨论】:

    • 我更新了问题以注册插件,但仍然出现错误
    • 没看到,因为我不知道这个库,但是 vue-cookies 使用$cookies 作为它的属性,而不是$cookie
    猜你喜欢
    • 1970-01-01
    • 2016-10-06
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    相关资源
    最近更新 更多