Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create back button using NuxtJS <nuxt-link />?

Tags:

nuxt.js

I have a fairly new Nuxt project I'm working on, and I'm running in to an issue setting up a back button. I even looked at the "vue-router-back-button" package which still doesn't want to work (I was getting unrelated errors). With the code that I have, the link wants to navigate to the same page that the user is currently on, rather than the one previous. I do receive an error on my server that there is an Invalid prop: type check failed for prop "to". Expected String, Object, got Function., however would I make the back button dynamic?

<template>
  <div class="page-title-wrapper">
    <nuxt-link
      v-if="back"
      :to="to"
      class="back-wrapper">
      <icon
        name="angle-left"
        fill="#9e9e9e"
        height="20px"
        width="20px"
        class="d-flex" />
      <p class="display-1 grey--text">
        Back
      </p>
    </nuxt-link>
    <h1 class="display-3 text-center">
      {{ text }}
    </h1>
  </div>
</template>

<script>
  export default {
    props: {
      back: {
        type: Boolean,
        default: false,
      },
      text: {
        type: String,
        default: 'Page'
      }
    },

    methods: {
      to() {
        this.$router.go(-1);     <---- evaluates to current page?
      },
    }
  }
</script>
like image 795
J. Jackson Avatar asked May 23 '20 05:05

J. Jackson


People also ask

How do I style a Nuxt link?

In order to style your active links the only thing you have to do to is add the nuxt-link-active class to your styles and then you can style it as you wish. Sometimes this will also add styles to other links such as the parent routes or the home page. In order to fix this we use the exact active class.

How does Nuxt link work?

Nuxt automatically includes smart prefetching. That means it detects when a link is visible, either in the viewport or when scrolling and prefetches the JavaScript for those pages so that they are ready when the user clicks the link.

How do I turn off Nuxt link?

Is to change the tag to the button and use the native :disabled state. Then just turn the button into the link with the help of styles. Awesome solution, thanks. Hey, @Samantha this should be the right answer.


2 Answers

this.$router.go(-1)

The command to go back one history step using Vue Router (which Nuxt uses):

this.$router.go(-1)

With a click listener, trigger it within a function, like so:

Template:

<div @click="goToPrev()">My Button</div>

Method:

methods: {
  goToPrev() {

    // ...
    // Do other logic like logging, etc.
    // ...

    // Tell router to go back one
    this.$router.go(-1);
  },
}

Another example:

Using the Vuetify framework and their button element (<v-btn>):

  <template>  
    <v-btn text :ripple="false" class="back-wrapper" @click="to">
      <icon name="angle-left" fill="#9e9e9e" height="20px" width="20px" class="d-flex" />
      <p class="display-1 grey--text">
        Back
      </p>
    </v-btn>
  </template>

  <script>
    methods: {
      to() {
        this.$router.go(-1)
      },
    }
  </script>
like image 193
J. Jackson Avatar answered Oct 18 '22 15:10

J. Jackson


You can also use this by checking window history length. If window history is 1 or less than 1 then it's going back to your home page. More usable.

 window.history.length > 1 ? this.$router.go(-1) : this.$router.push('/')
like image 24
Prerana Avatar answered Oct 18 '22 15:10

Prerana