【问题标题】:Vue warn: Error in created hook: "TypeError: Cannot read property 'get' of undefined"Vue警告:创建的钩子出错:“TypeError:无法读取未定义的属性'get'”
【发布时间】:2019-06-04 16:25:35
【问题描述】:

当我使用 axios 时出现此错误:

[Vue 警告]:创建钩子时出错:“TypeError: Cannot read property 'get' of undefined”

export default {
        methods: {
            loadUsers(){
                axios.get("api/user").then(data  => (this.users = data));
            }
        },
        created() {
            this.loadUsers();
        }
    }

路线: api.php

Route::apiResources(['user' => 'API\UserController']);

控制器: API/用户控制器.php

public function index()
    {
        return User::latest()->paginate(5);
    }

【问题讨论】:

  • 貌似axiosundefined,你导入了吗?
  • 哦,我导入这个:import {AxiosInstance as axios} from "axios"; 删除这个时,没有错误。

标签: javascript php laravel vue.js axios


【解决方案1】:

我遇到了同样的错误。这个错误是由于 vue 无法识别属于“vue-resource”包的 get 属性,所以我的建议是需要导入包,这将有助于解决这个问题,请按照以下步骤操作

首先检查项目中安装的“vue-resource” 否则安装它

npm install vue-resource

在 main.js 文件中包含这个

从 'vue' 导入 Vue 从'vue-resource'导入VueResource Vue.use(VueResource);

【讨论】:

    【解决方案2】:

    需要先导入Axios:

    import axios from 'axios'
    export default { 
        // ... axios.get will work now
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-11
      • 2020-04-13
      • 2021-02-06
      • 2021-12-22
      • 2021-02-17
      • 2021-06-04
      • 2018-07-14
      • 2021-03-21
      • 2021-08-18
      相关资源
      最近更新 更多