Vue.js变量变为未定义

时间:2018-08-31 05:25:55

标签: javascript html typescript vue.js

我已经在Vue.js中使用Typescript编写了此代码,但我无法弄清为什么在startLogin()中将其更改后,错误变量变为未定义。我尝试过直接在startLogin()中更改字符串,但是它仍然保持未定义状态。感谢您的帮助,我已经花了数小时盯着这个。如果我在startLogin()中执行console.log,则错误变量具有正确的值,但是当我在其他任何地方使用它时,它都是未定义的。

if (oldToken != null && !oldToken.equals(newToken) && newToken != null) {
        //Lets Delete the old Token, this is a blocking call and needs to happen in another thread
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    com.google.android.gms.iid.InstanceID.getInstance(PMApplication.getContext()).deleteInstanceID();
                } catch (IOException e) {
                    //Nothing can be done, in this case we have to live with 2 tokens and 2 messages
                }
            }
        }).start();
    }
<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>

1 个答案:

答案 0 :(得分:1)

您编写以下内容:

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

因此,基本上,您期望函数startLogin()返回一个数字值。
但是,在您当前的实现中,startLogin()没有返回值(即:startLogin(): void)。
为了定义error变量,您应该使startLogin()返回error的值,或者避免进行error = startLogin()赋值。