简体   繁体   中英

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

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);
    }

You need to import Axios first:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM