简体   繁体   English

Axios未在Vue中定义

[英]Axios is not defined in Vue

I'm trying to fetch data from Wordpress API. 我正在尝试从Wordpress API获取数据。 My console throws me an error "axios is not defined". 我的控制台抛出一个错误“axios未定义”。 Here is my post.vue component. 这是我的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>

I really have no idea what's wrong here, I installed axios, npm install axios, 我真的不知道这里有什么问题,我安装了axios,npm安装axios,

Here is my main.js file 这是我的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')

Can anybody help me with this? 任何人都可以帮我吗? I don't see where i got wrong? 我没看到我错在哪里?

Thank you all 谢谢你们

You need to add import axios from 'axios' to your component. 您需要将import axios from 'axios'添加到组件中。 Better yet create a config file called api.js file in your src directory and add something like this: 更好的是在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
});

Then in your components you import like this: 然后在您的组件中导入如下:

import API from '../api'

And do API.get instead of axios.get 并且做API.get而不是axios.get

This is benefical because: 这是有益的,因为:

  • You do not have to change your base url in 30 places when you need to change it. 当您需要更改基本网址时,您无需在30个位置更改基本网址。
  • You do not have to add the same headers over and over in your axios calls. 您不必在axios调用中反复添加相同的标头。
  • You can have shorter urls in your calls like this: 您可以在通话中使用更短的网址,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM