vue.js - 未捕获的类型错误:无法设置未定义的属性“axios”
问题描述
我只是尝试在 axios 和 Laravel 中最新的 Vue 的帮助下编写一个 SPA。我通过 npm 安装了所有需要的东西,但是我得到了标题中提到的错误。
app.js:19782 Uncaught TypeError: Cannot set property 'axios' of undefined
我查看了我的组件 app.js 和 bootstrap.js,但对我来说一切都很正常。我什至在网上搜索了解决方案,遗憾的是他们没有提供帮助。
我的开发服务器在后台运行,所以应该有一个工作连接(工匠服务)。
应用程序.js
require('./bootstrap');
window.Vue = require('vue');
import App from './App.vue';
import VueRouter from 'vue-router';
import VueAxios from 'vue-axios';
import axios from 'axios';
import {routes} from './routes';
Vue.use(VueRouter);
Vue.use(VueAxios, axios);
const router = new VueRouter({
mode: 'history',
routes: routes
});
const app = new Vue({
el: '#app',
router: router,
render: h => h(App),
});
引导程序.js
window._ = require('lodash');
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
AllBooks.vue
<template>
<div>
<h3 class="text-center">All Books</h3>
<br />
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Author</th>
<th>Created At</th>
<th>Updated At</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr v-for="book in books" :key="book.id">
<td>{{ book.id }}</td>
<td>{{ book.name }}</td>
<td>{{ book.author }}</td>
<td>{{ book.created_at }}</td>
<td>{{ book.updated_at }}</td>
<td>
<div class="btn-group" role="group">
<router-link
:to="{ name: 'edit', params: { id: book.id } }"
class="btn btn-primary"
>Edit
</router-link>
<button
class="btn btn-danger"
@click="deleteBook(book.id)"
>
Delete
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
data() {
return {
books: []
};
},
created() {
this.axios.get("http://localhost:8000/api/books").then(response => {
this.books = response.data;
});
},
methods: {
deleteBook(id) {
this.axios
.delete(`http://localhost:8000/api/book/delete/${id}`)
.then(response => {
let i = this.books.map(item => item.id).indexOf(id); // find index of your object
this.books.splice(i, 1);
});
}
}
};
</script>
解决方案
尝试将 axios 添加到 Vue 实例。
阅读本文了解更多详情https://vuejs.org/v2/cookbook/adding-instance-properties.html
在你的 app.js
require('./bootstrap');
window.Vue = require('vue');
import App from './App.vue';
import VueRouter from 'vue-router';
import VueAxios from 'vue-axios';
import axios from 'axios';
import {routes} from './routes';
Vue.use(VueRouter);
Vue.use(VueAxios, axios);
Vue.prototype.$axios = axios; // notice this
const router = new VueRouter({
mode: 'history',
routes: routes
});
const app = new Vue({
el: '#app',
router: router,
render: h => h(App),
});
然后在你的 AllBooks.vue
<script>
export default {
data() {
return {
books: []
};
},
created() {
this.$axios...;
},
methods: {
deleteBook(id) {
this.$axios...;
}
}
};
</script>
推荐阅读
- vba - 在访问 vba 中,如果我尝试手动步进代码,它会工作 2-3 行,然后执行其余的不步进
- node.js - TypeError: (depth0 || container.hooks.helperMissing).call 不是函数
- javascript - Next js:将钩子作为参数传递给另一个文件中存在的函数是否很好?
- django - PUT方法中django rest框架的html形式没有CSRF输入字段
- postgresql - 如何在 PostgreSQL SELECT 语句中使用自定义类型枚举?
- python - 如何提示输入直到日期格式和时间格式正确?
- r - 使用 R (CRAN) 在多列中以特定模式分隔
- angular - Angular 单元测试 - 使用两个 beforeEach() 方法(一个同步和一个异步),它们是否总是以正确的顺序执行/解决?
- node.js - 在 HTTP 请求中解析 JSON 中的图像
- r - 在 ggplot 组合中绘制 gamma、normal 和 t 的 PDF