[Vuejs]-"TypeError: Cannot read property 'push' of undefined" Vuejs

1👍

Vue-Router will not work in custom js file file like User.js

Use it in Vue Component. Like:

Register.vue

<template>
  <div class="col-md-12">
    <div class="card card-container">
      
      <form name="form" @submit.prevent="handleRegister">
       ----
      </form>
   
  </div>
</template>
<script>
export default {
   data() { return { user: {} }},
   
     methods: {
    handleRegister() {
        this.registration();
      });
    },

    registration() {
        User.register(this.user);
        this.$router.push({ name: 'Index'});
    }
  },
   
}
</script>

0👍

The other answer is correct but what the case of the registration failure !, so try to add a callback as parameter then call it inside then :

class User {
    register(data,cb) {
        let uri = 'http://localhost:8000/api/auth/register';
        let self = this;
        Axios.post(uri, data).then((res) => {
            cb()
        }).catch( (error) => {
            console.log(error);
        });
    }
}
   registration() {
        User.register(this.user, ()=>{this.$router.push({ name: 'Index'});});
        
    }

Leave a comment