0

Hello I want to redirect routing my login component page to the dashboard component with successful login. I have done a response through my local API having a status code 400 & status code 200 but don't know how to redirect after the successfully login.

Here is the component script code:
    import axios from 'axios'
    export default {
      name: 'Login',
      data () {
        return {
          email: '',
          password: '',
          error: ''
        }
      },
      methods: {
        login () {
          axios.post('http://ztlab01/db-admin/app/v1/login', {
            email: this.email,
            password: this.password
          }, {'Access-Control-Allow-Origin': '*'})
            .then((response) => {
              console.log(response.data)
              if (response.data.code === 400) {
                this.error = 'Invalid Credentials'
              }
              if (response.data.code === 200) {
                this.error = ''
              }
            })
            .catch(error => {
              console.log(error)
            })
        }
      }
    }
Mittal Shah
  • 1
  • 1
  • 4
  • Possible duplicate of [How do I redirect to another webpage?](https://stackoverflow.com/questions/503093/how-do-i-redirect-to-another-webpage) – timiTao Mar 17 '18 at 06:49
  • here i am using Vue.js so i need to redirect in a component of vue.js like Login.vue to Dashboard.vue after succefully login response – Mittal Shah Mar 17 '18 at 06:52
  • Possible duplicate of [Redirect to requested page after login using vue-router](https://stackoverflow.com/questions/45856929/redirect-to-requested-page-after-login-using-vue-router) –  Mar 17 '18 at 11:32

1 Answers1

1

If you are using vue-router, you should use router.go(path) (VueJS < 2.0) to navigate to any particular route.

The router can be accessed from within a component using this.$router.

router.go() changed in VueJS 2.0. You can use router.push({ name: "yourroutename"}) or just router.push("yourroutename") now to redirect.

Otherwise, window.location.href = 'some url'; works fine for non single-page apps.

More links to help

https://router.vuejs.org/en/essentials/redirect-and-alias.html

Vue.js redirection to another page

timiTao
  • 1,417
  • 3
  • 20
  • 34
MyTwoCents
  • 7,284
  • 3
  • 24
  • 52