简体   繁体   中英

vuejs displaying API data using attribute binding

I have a simple app where a user selects a Person and vue makes an api call for that users posts.Each of these posts in turn have their own comments.This is all from https://jsonplaceholder.typicode.com/ The comment section is always empty.

The codepen is here

My html

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<div id='app'>
<div v-if='isError'>
There was an error
</div>
<div v-else-if='isLoading'>
Loading
</div>
<div v-else>
 <select v-model="selectedUser">
      <option v-for="person in info" v-bind:value="person"> {{person.name}}</option>
 </select>
</div>
<div class="posts">
  <div v-if="isLoadingPosts">
  Loading...
  </div>
  <div v-else>
    <ul>
      <li v-for="post in postData">
      <p>
      {{ post.body }}
      </p>
       <button v-bind:id='post.id' v-on:click='getComments'>
      View Comments
      </button>
      </li>

    </ul>
  </div>
</div>
<div class="comments">
<p>
{{ commentData }}
</p>
</div>
</div>

JS logic

var app = new Vue({
  el: '#app',
  data: {
    info : null,
    isLoading : true,
    isError : false,
    selectedUser : '',
    postData : null,
    isLoadingPosts : true,
    commentData : null,
  },
  watch : {
  selectedUser : function () {
  axios
  .get('https://jsonplaceholder.typicode.com/posts?userId=' + this.selectedUser.id)
  .then(response => (this.postData =response.data))
  .catch(error=> {console.log(error)})
  .finally(() => this.isLoadingPosts = false)
   }
  },
  methods : {
  getComments : function (){
    axios
        .get('https://jsonplaceholder.typicode.com/posts/' + this.id + '/comments')
      .then(response => (this.commentData =response.data))
  }
  },
  mounted () {
  axios
    .get('https://jsonplaceholder.typicode.com/users')
    .then(response => (this.info = response.data))
    .catch(error => {console.log(error);this.isError = true})
    .finally(() => this.isLoading = false)
  }
})

Everything works except the comments part where it always returns an empty object.I also feel that my code is repetitive,any corrections would be appreciated.

So you have a couple issues with this method:

getComments : function (){
    axios
        .get('https://jsonplaceholder.typicode.com/posts/' + this.id + '/comments')
      .then(response => (this.commentData =response.data))
    }
  }

First, this.id in there will be looking for an id prop on the component itself, not the id you're trying to bind in your button.

Try changing the button code to this:

<button v-on:click='getComments(post.id)'>View Comments</button>

And then the method to:

  getComments : function (id){
    axios
        .get('https://jsonplaceholder.typicode.com/posts/' + id + '/comments')
      .then(response => (this.commentData =response.data))
    }
  }

Also, you might want to add a .catch() handler like you id for your other axios calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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