1
votes

When I use axios I got this error:

[Vue warn]: Error in created hook: "TypeError: Cannot read property 'get' of undefined"

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

Routes: api.php

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

Controller: API/UserController.php

public function index()
    {
        return User::latest()->paginate(5);
    }
2
Looks like axios is undefined, did you import it?thanksd
Ohh I import this: import {AxiosInstance as axios} from "axios"; When remove this, there has no error.Md. Nayem

2 Answers

3
votes

You need to import Axios first:

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

I was getting same error .this error comes due to the vue cannot identify the get property which is belongs to the 'vue-resource' package so my recommendation is need to import package it will help to resolse this issue follow below step

first check 'vue-resource' installed on your project otherwise install it

npm install vue-resource

In main.js file include this

import Vue from 'vue' import VueResource from 'vue-resource' Vue.use(VueResource);