Skip to content
the poor coder

nuxt router go back

Poor Man
Published
Reading time
2 min read
On this page

Nuxt Router Go Back#

If you are building a web application using Nuxt.js, there may come a time where you need to implement a "go back" functionality. This is where the user can click a button and be taken back to the previous page they were on. Luckily, the Nuxt.js router makes this relatively easy to implement.

Using the router's go method#

The simplest way to implement a "go back" button in Nuxt.js is to use the router's go method. This method takes a single argument, which is the number of pages to go back in the history. So, to go back one page, you would call router.go(-1).

Here's an example implementation:

JavaScript

<template>
  <div>
    <button @click="goBack">Go back</button>
  </div>
</template>

<script>
export default {
  methods: {
    goBack() {
      this.$router.go(-1);
    }
  }
}
</script>

In this example, we have a button that calls the goBack method when clicked. This method simply calls this.$router.go(-1), which takes us back one page in the history.

Using the router's back method#

The router also has a back method, which does the same thing as calling go(-1). This method can be useful if you want to provide a more descriptive label for your "go back" button.

Here's an example implementation:

JavaScript

<template>
  <div>
    <button @click="goBack">Go back</button>
  </div>
</template>

<script>
export default {
  methods: {
    goBack() {
      this.$router.back();
    }
  }
}
</script>

As you can see, the goBack method now calls this.$router.back() instead of this.$router.go(-1).

Finally, you can also implement a "go back" button using the router-link component. This component is used to create links between pages in your application, but it can also be used to create buttons that navigate between pages.

Here's an example implementation:

HTML

<template>
  <div>
    <router-link to="/" tag="button">Go back</router-link>
  </div>
</template>

In this example, we're using the router-link component to create a button that navigates back to the home page of our application. The tag attribute is used to specify that we want to render a button element instead of an a element.

So there you have it - three different ways to implement a "go back" button in Nuxt.js. Choose the method that works best for your particular use case!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC