简体   繁体   English

使用Vuejs从Wordpress Api获取帖子

[英]Fetch posts from Wordpress Api with Vuejs

I'm trying to fetch posts title form WP API with Vuejs but console throws me an error 我正在尝试使用Vuejs获取帖子标题形式的WP API,但控制台抛出一个错误

Cannot read property 'rendered' of undefined" 无法读取未定义的“已渲染”属性”

I don't know what is a problem. 我不知道有什么问题。 Here is posts component: 这是帖子部分:

<template>

<div class="posts">
            <h1>Posts</h1>
            <ul>
                <li v-for="post in posts" :key="post.id">
                     {{ post.title.rendered  }}
                </li>
            </ul>
        </div>
</template>

<script>

    export default {
        mounted() {
            this.getPosts();
        },

        data() {
            return {
                postsUrl: 'http://localhost:8080/wp-json/wp/v2/posts',
                posts: [],
                postsData: {
                    per_page: 10,
                    page: 1
                },

            }
        },
        methods: {

            getPosts() {
              axios.get(this.postsUrl, {params: this.postsData})
                    .then((response) => {
                        this.posts = response;
                          this.configPagination(response.headers);
                    })
                    .catch( (error) => {
                        console.log(error);
                    });
            },

        }
    }
</script>

the response object in axios includes multiple properties like headers , status and data , in your case the your posts are the data property, so set this.posts = response.data; axiosresponse对象包含多个属性,例如headersstatusdata ,在您的情况下,您的帖子是data属性,因此设置this.posts = response.data; :

     .then((response) => {
                    this.posts = response.data;
                      this.configPagination(response.headers);
                })

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

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