laravel - laravel vue 插件使用方法
问题描述
我正在尝试 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 当我单击按钮时
解决方案
Vue.js文档告诉您在初始化 Vue 实例之前注册插件。
在您的应用程序的某个时刻,您会创建一个new Vue(...)
实例。您需要在Vue.use(VueCookies)
此之前调用,而不是在组件的文件中调用。由于在导入组件时 Vue 可能已经实例化,因此$cookie
尚未设置属性。
推荐阅读
- python - 将 beautifulsoup 输出转换为字典
- sql - SQL:使用 UPDATE SET 设计新功能
- google-apps-script - != "TEXT" 无法使用 Google Sheet 脚本
- nosql - 我可以将哪些区域代码用于 Oracle NoSQL 数据库云服务 SDK 中的 NoSQLHandleConfig?
- angular - 当我更新角度 Ag-grid 表中的单个单元格时(使用 NgRx 存储),整个表被重新渲染
- regex - 从 PostGreSQL 11.0 中的列中提取特定字段
- android - 从以前的应用程序安装中删除所有内容
- javascript - 带有下拉菜单的引导选项卡在选择时未突出显示
- html - 响应 :hover 的 CSS 元素
- python - 迭代特定的 XML 元素和命名空间问题