javascript - Vue中没有定义axios

标签 javascript vue.js axios vue-component

我正在尝试从 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')

谁能帮我解决这个问题?我不明白我哪里错了?

谢谢大家

最佳答案

您需要将 import axios from 'axios' 添加到您的组件中。最好在你的 src 目录中创建一个名为 api.js 的配置文件并添加如下内容:

import axios from 'axios';

export default axios.create({
    baseURL: `http://127.0.0.1:8000/`,
    headers: {
        'Content-Type': 'application/json',
        'Authorization': "JWT " + localStorage.getItem('token')
    },
    xsrfCookieName: 'csrftoken',
    xsrfHeaderName: 'X-CSRFToken',
    withCredentials: true
});

然后在你的组件中像这样导入:

import API from '../api'

并执行 API.get 而不是 axios.get

这是有益的,因为:

  • 当您需要更改基本 URL 时,您不必更改 30 个地方。
  • 您不必在 axios 调用中反复添加相同的 header 。
  • 您可以像这样在通话中使用更短的 url:

    API.get('foo/bar/') .then(响应=> { }

关于javascript - Vue中没有定义axios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53093902/

相关文章:

javascript - IPython:将 Javascript 脚本添加到 IPython notebook

javascript - 在 vuejs 模板中使用样式标签并从数据模型更新

header - 使用 Axios 获取请求的 "Content-Disposition"Header

node.js - 具有 native react 的 Axios 不返回文档并使应用程序崩溃

javascript - 无法在 map 循环中访问 Axios 调用的值

javascript - 谷歌地图V3 : fitbounds and center not working together

javascript - 如何使用 Three.js 和 Orbit-controls 添加对主视图的移动使用react的覆盖部分?

javascript - 在 D3 力导向图中突出显示所选节点、其链接及其子节点

javascript - Vue+Laravel : How to mounted data from api if data in form array in one of tuple data

vue.js - Nuxt.js + Bootstrap-Vue - 单独的组件和指令加载