Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js Routing with back button

I already use this Vue.js Routing example application.

https://github.com/chrisvfritz/vue-2.0-simple-routing-example

In the src/main.js I have so much data value .

const app = new Vue({
  el: '#app',
  data: {
    currentRoute: window.location.pathname,
    token : "",
    errorMessage : '', etc...etc..
  },

Now with socket.io i set the token to "sdawdda2d2ada2ad22ad"

When application start than the currentRoute is equal with "/" Its okey, the first page loaded. src/routes.js

'/': 'Home',
'/about': 'About'

When i want to check the /about (url: localhost:8080/about), than its works good , but the token and errorMessage is empty again, because the app created again.

If i want to change page without lose the token value i can use:

this.currentRoute = "/about"       (url: localhost:8080)

Its works good , but the url not change, so i cant use back button in browser. How can i separate my Vue app, if i dont want to lose token value when the browser go to /about?

Thanks so much!

like image 266
ketom Avatar asked Oct 21 '16 17:10

ketom


People also ask

How do I go back to previous route on Vue?

In Vue. js you can take advantage of the Vue Router methods. You can use router.go(n) method to programmatically navigate backwards or forwards. It takes an integer as the only parameter that tells the method how many steps to go forward or backwards.

Does Vue JS have routing?

Vue. js provides a bunch of features that allow you to build reusable web components. Routing is one of those methods. It allows the user to switch between pages without refreshing the page.

What is router beforeEach?

We can use router. beforeEach to listen for routing events globally across the application. This is worth using if you have authentication checks or other pieces of functionality that should be used in every route. Here's an example that simply logs out the route the user is going to and coming from.


1 Answers

simple way

<template lang="html">
  <div>   
    <button @click="goBack">
      Back
    </button>
  </div>
</template>
<script>
  export default {
    methods: {
      goBack() {
        this.$router.go(-1)
      }
    }
  }
</script>
like image 125
hama99o Avatar answered Sep 28 '22 04:09

hama99o