在一个 Laravel + Vue 项目中,我尝试使用axios来获取API响应。 Axios 调用Laravel端点并获得控制器响应。
代码看起来
JS
require("./bootstrap");
import Swal from "sweetalert2";
import VueI18n from "vue-i18n";
import VueRouter from "vue-router";
import axios from "axios";
import Vuetify from "vuetify";
import es from "vuetify/es5/locale/es";
import en from "vuetify/es5/locale/en";
import "@mdi/font/css/materialdesignicons.css";
import ContadoresComponent from "./components/ContadorComponent.vue";
import GatewaysComponent from "./components/GatewayComponent.vue";
import MainComponent from "./components/MainComponent.vue";
const routes = [{
path: "/",
name: "principal",
component: MainComponent
},
{
path: "/contadores",
name: "contadores",
component: ContadoresComponent
},
{
path: "/gateways",
name: "gateways",
component: GatewaysComponent
}
];
window.Vue = require("vue");
Vue.use(Vuetify);
Vue.use(VueRouter);
Vue.use(VueI18n);
Vue.use(axios);
Vue.component(
"drawer-component",
require("./components/DrawerComponent.vue").default
/* methods: {
changeLocale (lang) {
this.$vuetify.lang.current = lang
},
},*/
);
Vue.component(
"table-component",
require("./components/TableComponent.vue").default
);
export default new Vuetify({
icons: {
iconfont: "mdi"
},
lang: {
locales: {
es,
en
},
current: "es"
}
});
const router = new VueRouter({
routes
});
new Vue({
vuetify: new Vuetify(),
router
}).$mount("#app");
Vue(Vuetify)
<template>
<v-container class="fill-height" fluid>
<v-row align="center" justify="center">
<v-card class="mx-auto">
<v-list-item>
<v-list-item-content>
<v-list-item-title class="headline">Contador</v-list-item-title>
</v-list-item-content>
</v-list-item>
<table-component></table-component>
</v-card>
</v-row>
</v-container>
</template>
<script>
axios.get("/contadores").then(response => console.log(response));
</script>
错误:未定义Return axios,但我认为我在App.js文件中定义了。
有人看到错误吗?
答案 0 :(得分:1)
您仍然需要将其导入第二个文件。您的Scrip标记应如下所示:
<script>
import axios from 'axios';
axios.get("/contadores").then(response => console.log(response));
</script>
答案 1 :(得分:0)
您尚未在文件中导入axios。
要解决此问题,请按照以下说明将其导入您要使用的文件中
import axios from 'axios';
OR
如果您不想在使用它的每个组件中导入它,则可以将其添加到Vue原型中:
window.axios = require('axios');
//... configure axios...
Vue.prototype.$http = window.axios;
然后在您可以使用的任何组件中
this.$http.post('event/store', {
'name': this.name,
})
答案 2 :(得分:0)
我在项目中使用的一种有趣的方法是使用库vue-axios,该库的安装非常简单。如果需要通过命令npm install --save axios vue-axios
在项目中安装依赖项,然后将其导入到main.js
文件或应用程序的主文件中:
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use (VueAxios, axios)
将通过this.axios.get(`${url}`)
在组件中使用axios。朋友已经在StackOverflow此处公开的另一种方法是将axios直接导入到您要使用的组件中:
<template>
<div>
...
</div>
</template>
<script>
import axios from 'axios';
export default {
name: 'ComponentName',
methods: {
async getAsyncApi() {
try {
const result = await axios.get(`${url}`);
} catch(e) {
window.console.error('Error! ', e.message);
}
},
getApi() {
let result;
axios.get(`${url}`).then((r) => {
result = r;
}).catch(e => window.console.error('Error! ', e.message));
},
},
};
</script>
答案 3 :(得分:0)
我通过以下方式纠正错误
export default {
mounted() {
axios
.get("ENDPOINT")
.then(response => console.log(response));
}
};