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

When I use axios I got this error:[Vue warn]: Error in created hook: "TypeError: Cannot read prope

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

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);
    }
Share Improve this question asked Jan 9, 2019 at 20:30 Md. NayemMd. Nayem 411 silver badge4 bronze badges 2
  • Looks like axios is undefined, did you import it? – thanksd Commented Jan 9, 2019 at 20:44
  • Ohh I import this: import {AxiosInstance as axios} from "axios"; When remove this, there has no error. – Md. Nayem Commented Jan 9, 2019 at 20:52
Add a ment  | 

2 Answers 2

Reset to default 3

You need to import Axios first:

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

I was getting same error .this error es due to the vue cannot identify the get property which is belongs to the 'vue-resource' package so my remendation 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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744869612a4598178.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信