【发布时间】:2022-01-26 20:47:40
【问题描述】:
强文本 我是 laravel 和 vue js 的新手。我正在尝试学习 Vue js。在 Laravel+Vue 项目中,我尝试使用 axios 发布 API 响应。 Vue js 2中没有定义axios。如何解决这个问题。当我添加一些数据时。数据没有显示,也没有我的删除功能。为什么我面临这个问题?感谢提前
app.js
import Vue from 'vue';
import App from './vue/app';
import { library } from '@fortawesome/fontawesome-svg-core'
import { faPlusSquare, faTrash } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
library.add(faPlusSquare, faTrash)
Vue.component('font-awesome-icon', FontAwesomeIcon)
const app = new Vue ({
el: '#app',
components: { App }
});
addItemForm
<template>
<div class="addItem">
<input type="text" v-model="item.name" />
<font-awesome-icon
icon="plus-square"
@click="addItem()"
:class="[item.name ? 'active' : 'inactive', 'plus']"
/>
</div>
</template>
<script>
export default {
data: function () {
return {
item: {
name: "",
},
};
},
methods: {
addItem() {
if (this.item.name == "") {
return;
}
axios
.post("api/item/store", {
item: this.item,
})
.then((response) => {
if (response.status == 201) {
this.item.name = "";
this.$emit("reloadlist");
}
})
.catch((error) => {
console.log(error);
});
},
},
};
</script>
<style scoped>
.addItem {
display: flex;
justify-content: center;
align-content: center;
}
input {
background: rgb(236, 164, 138);
border: 0px;
outline: none;
padding: 5px;
margin-right: 10px;
width: 100%;
}
.plus {
font-size: 20px;
}
.active {
color: rgb(34, 211, 57);
}
.inactive {
color: rgb(63, 66, 63);
}
</style>
app.vue
<template>
<div class="todoListContainer">
<div class="heading">
<h2 id="title">Todo List</h2>
<add-item-form v-on:reloadlist="getList()" />
</div>
<list-view :items="items"
v-on:reloadlist="getList()" />
</div>
</template>
<script>
import addItemForm from "./addItemForm.vue";
import listView from "./listView.vue";
export default {
components: {
addItemForm,
listView,
},
data: function () {
return {
items: [],
};
},
methods: {
getList() {
axios
.post('api/items')
.then((response) => {
this.items = response.data;
})
.catch((error) => {
console.log(error);
});
},
},
created() {
this.getList();
},
};
</script>
<style scoped>
.todoListContainer {
width: 350px;
margin: auto;
}
.heading {
background: wheat;
padding: 10px;
}
#title {
text-align: center;
}
</style>
【问题讨论】:
-
您尚未在
addItemForm.vue和app.vue文件中导入axios
标签: javascript laravel vue.js