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
isundefined
, 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
2 Answers
Reset to default 3You 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条)