【问题标题】:Laravel-mix in Laravel-7 and vue.js error:(Uncaught Type Error: Cannot set property '$Gate' of undefined)Laravel-7 和 vue.js 中的 Laravel-mix 错误:(未捕获类型错误:无法设置未定义的属性 '$Gate')
【发布时间】:2021-07-13 21:51:59
【问题描述】:

我只是一名学生,并且是使用 Laravel 和 vue.js 的新手。我在 laravel-7 中使用 Laravel-mix 6.0 版,在运行 mix 后,我在 public/js/app.js.console error description 中收到此错误(未捕获类型错误:无法设置未定义的属性 '$Gate')。因此,路由不起作用,并且可见禁用。

app.js 中的错误行:

Vue.prototype.$gate = new _Gate__WEBPACK_IMPORTED_MODULE_2__.default(window.user);
我的资源/资产/js/app.js 文件包含以下代码:

import Gate from "./Gate";
Vue.prototype.$gate = new Gate(window.user);

laravel-mix webpack 应用在这个文件上。

我的 gates 文件包含以下代码:

export default class Gate{

    constructor(user){
        this.user = user;
    }


    isAdmin(){
        return this.user.type === 'admin';
    }

    isUser(){
        return this.user.type === 'user';
    }
    isAdminOrAuthor(){
        if(this.user.type === 'admin' || this.user.type === 'author'){
            return true;
        }

    }
    isAuthorOrUser(){
        if(this.user.type === 'user' || this.user.type === 'author'){
            return true;
        }

    }
}

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: php laravel vue.js laravel-7 laravel-mix


    【解决方案1】:

    试试这个:

    import Gate from "./Gate";
    
    const $gate = new Gate(window.user);
    
    Vue.prototype.$gate = $gate;

    【讨论】:

      【解决方案2】:

      你必须在调用原型之前导入 Vue
      通过你的app.js:上方的这个命令
      import Vue from 'vue/dist/vue'

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-20
        • 2017-03-30
        • 2011-03-11
        • 2013-06-16
        • 2016-11-08
        • 2012-05-27
        相关资源
        最近更新 更多