0

I am creating a button that loads more content on click. At the moment, on click, it changes the text from "Load More" to "Loading" and disables the button. But, now the button still says "Loading" and is still disabled, so I can't click it again.

I have the inner text of the button in the "data" of my component, but that may not be the best way.

Here is the HTML:

      <Button
        v-if="allowLoadMore && morePagesAvailable"
        :loading="loading"
        :disabled="clicked"
        @click="loadMore"
      >
        {{ loadMoreText }}
      </Button>

Here is the data:

  data () {
    return {
      page: 1,
      morePagesAvailable: false,
      loadMoreText: 'Load More',
      clicked: false
    }

Here is the method:

  methods: {
    loadMore () {
      if (this.morePagesAvailable) {
        this.page += 1
        ...
        this.loadMoreText = 'Loading'
        this.clicked = true
        return this.loadMoreText
      }
    }
  }

I want to disable the button and show "loading" only when it is actually loading, so that I can click until I have no more pages available.

Angello L.
  • 101
  • 1
  • 2
  • 9

1 Answers1

0

Assuming you use something like axios, you should create something like this

loadMore () {
      if (this.morePagesAvailable) {
        this.page += 1
        ...
        this.loadMoreText = 'Loading'
        this.clicked = true
        Axios.get(...).then( resp => {
              //use the resp
              this.clicked = false
       }) 
        return this.loadMoreText
      }
    }

Also you should probably rename clicked to isLoading.

blackening
  • 903
  • 6
  • 14