Vue.js variable turns undefined

Viewed 1678

I've written this in Vue.js with Typescript and I can't figure out why the error variable turns undefined after it's been changed in startLogin(). I tried before to change the string directly in startLogin() but it still kept turning undefined. Any help is appreciated, I've spent hours staring at this. If I do console.log in startLogin() the error variable has the correct value but when I use it anywhere else it's undefined.

<script lang="ts">
import App from "./App.vue";
import router from "./router";
import store from "./store";
import "./registerServiceWorker";
import { Component, Prop, Vue } from "vue-property-decorator";

@Component
export default class Login extends Vue {
    error: number;
    username: string;
    password: string;

    constructor(){
        super();
        this.error = 0;
        this.username ="";
        this.password ="";
    }

    startLogin(): void {
        if(this.$data.username != "" && this.$data.password != "") {
            if(this.$data.username == "michaela") {
                this.$emit("authenticated", true);
                new Vue({
                    router,
                    store,
                    render: h => h(App)
                }).$mount("#login");
            } else {
                this.$data.error = 1;
            }
        } else {
            this.$data.error = 2;
        }
    }

   get errorMsg() {
        console.log(this.$data.error);
        if(this.$data.error == 0){
            return "Please enter your username and password";
        } else if(this.$data.error == 1){
            return "The username or password is incorrect";
        } else if(this.$data.error == 2){
            return "Both username and password must be present";
        }
    }
}
</script>
<style lang="scss">
#login {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

h1 {
  font-size: 30px;
  padding: 0;
}

form {
  color: #2c3e50;
  color: white;
  padding: 0;
  margin: auto;
  width: 300px;
}

form h1 {
  margin: auto;
  color: #2c3e50;
}

input, select, textarea {
    display: inline-block;
    width: 70%;
    padding: 16px 20px 10px 10px;
    border: none;
    background-color: lightseagreen;
    color: white;
    margin: auto;
}

input {
  height: 10px;
  font-size: 15px;
}

textarea {
  height: 30px;
}

label, form p {
    display: block;
    margin: 10px 0px 0px 0px;
}

form .button, button {
    height: 30px;
    width: 80px;
    text-align: center;
    padding: 0;
    background-color: lightseagreen;
    border: none;
    color: white;
    margin: auto;
}

::placeholder {
    color: white;
}

.error {
    color: red;
}
</style>
<template>
    <div id="login">
        <h1>Login</h1>
        <p :class="{'error' : error != 0}">{{errorMsg}}</p>
        <form>
            <p><input type="text" name="username" v-model="username" placeholder="Username" /></p>
            <p><input type="password" name="password" v-model="password" placeholder="Password" /></p>
            <p><button type="button" v-on:click="error = startLogin()">Submit</button></p>
        </form>
    </div>
</template>

1 Answers

You write the following:

<button type="button" v-on:click="error = startLogin()">Submit</button></p>

so basically you're expecting that the function startLogin() returns a number value.
However, in your current implementation, startLogin() has not a return value (i.e.: startLogin(): void).
In order to make error variable defined, then, you should make either startLogin() returning the error value or avoiding to do the error = startLogin() assignment.

Related