【发布时间】:2019-04-05 05:48:28
【问题描述】:
我正在尝试从 Wordpress API 获取数据。我的控制台向我抛出一个错误“未定义 axios”。 这是我的 post.vue 组件。
<template>
<div>
<table class="table is-bordered">
<thead>
<tr>
<th>Title</th>
<th>Posted at</th>
</tr>
</thead>
<tbody>
<tr v-for="post in posts" :key="post.id">
<td>{{post.title.rendered}}</td>
<td>{{post.date_gmt}}</td>
</tr>
</tbody>
</table>
<pagination :pagination="pagination"
@prev="--postsData.page; getPosts();"
@next="postsData.page++; getPosts();">
</pagination>
</div>
</template>
<script>
import pagination from './pagination.vue'
export default {
mounted() {
this.getPosts();
},
components: {
'pagination': pagination
},
data() {
return {
postsUrl: 'http://localhost:8080/wp-json/wp/v2/posts',
posts: [],
postsData: {
per_page: 10,
page: 1
},
pagination: {
prevPage: '',
nextPage: '',
totalPages: '',
from: '',
to: '',
total: '',
},
}
},
methods: {
getPosts() {
axios
.get(this.postsUrl, {params: this.postsData})
.then((response) => {
this.posts = response;
this.configPagination(response.headers);
})
.catch( (error) => {
console.log(error);
});
},
configPagination(headers) {
this.pagination.total = +headers['x-wp-total'];
this.pagination.totalPages = +headers['x-wp-totalpages'];
this.pagination.from = this.pagination.total ? ((this.postsData.page - 1) * this.postsData.per_page) + 1 : ' ';
this.pagination.to = (this.postsData.page * this.postsData.per_page) > this.pagination.total ? this.pagination.total : this.postsData.page * this.postsData.per_page;
this.pagination.prevPage = this.postsData.page > 1 ? this.postsData.page : '';
this.pagination.nextPage = this.postsData.page < this.pagination.totalPages ? this.postsData.page + 1 : '';
}
}
}
</script>
我真的不知道这里出了什么问题,我安装了axios,npm install axios,
这是我的 main.js 文件
import Vue from 'vue'
import App from './App.vue'
import posts from "./components/posts.vue";
import axios from "axios";
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
Vue.prototype.$axios = axios;
Vue.component('posts', posts);
Vue.config.productionTip = false
new Vue({
render: h => h(App),
}).$mount('#app')
谁能帮我解决这个问题?没看明白哪里错了?
谢谢大家
【问题讨论】:
-
你运行过'npm install axios --save'吗?
-
谢谢,现在我有错误“无法读取未定义的'rendered'属性”?
-
你还需要将 axios 导入到你的 vue 组件中。
标签: javascript vue.js axios vue-component