[Vuejs]-WordPress REST API not working when filtering by slug using Axios

0πŸ‘

It is necessary to check whether there is cross domain problem in the console, and there may be no cross domain problem

0πŸ‘

I figured it out a solution. In my links to an individual post I carried over the slug and id as params

<router-link :to="{name: 'ShowPost', params: {slug: post.slug, id:post.id}}">{{ post.title.rendered }}</router-link>

Route is slug and filter response by id. It now works as planned.

import axios from "axios";

export default {
  name: 'ShowPost',
  data () {
    return {
      post: []
    }
  },
  created() {
           this.id = this.$route.params.id;
       },
  mounted() {
           axios({ method: "GET", "url": "https://wpdemo.stevensoehl.com/wp-json/wp/v2/posts/" + this.id }).then(json => {
               this.post = json.data;
           }, error => {
               console.error(error);
           });
       }
}

Leave a comment