jeudi 15 octobre 2020

How can I generate random item on button click?

I have a component that generates a random item every time I access the random page. Each time I refresh the page a new random item is generated. I would like to generate a new random item every time I click the button 'pick another', but I'm not entirely sure how this can be done. Any help would be greatly appreciated.

Random.vue

<template>
  <div class="details" v-for="item in items" v-bind:key="item.id">
    <div class="details-primary u-center-text">
      <h1 class="heading-secondary"></h1>
      <p class="tagline--main"></p>
    </div>
    <div class="details-secondary u-margin-top-big">
      <div class="info">
        <span class="info__detail info--title">Vol</span>
        <span class="info__detail info--spec">%</span>
      </div>
      <img class="details-image" :src='item.image_url' alt="">
      <div class="info">
        <span class="info__detail info--title">Amount</span>
        <span class="info__detail info--spec">1ltr</span>
      </div>
    </div>
  </div>
  <div class="rand-gen">
    <a href="" class="btn">Pick another</a>
  </div>
</template>

<script lang="ts">
import {Options, Vue} from 'vue-class-component'
import axios from 'axios';

@Options({
  data() {
    return{
      items: []
    }
  },

  mounted() {
    axios.get('https://api.punkapi.com/v2/beers/random')
    .then(res => this.items = res.data)
    .catch(err => console.log(err));
  }
})

export default class Random extends Vue {}
</script>




Aucun commentaire:

Enregistrer un commentaire