javascript - vuejs this.$router.push()無效
世界只因有你
世界只因有你 2017-05-19 10:22:22
0
2
551
login() {
        if(this.email.length > 0 && this.password.length >0) {
            this.$http.post('/api/login', {
                user: this.email,
                password: this.password
            })
            .then(res => {
                let userPwd = res.data
                if(this.password == userPwd) {
                    this.$router.push("/")
                } else {
                    alert("错误,请重新输入!")
                }
            })
            .catch(err => {
                console.log(err)
            })
        } else {
            alert("输入错误!")
        }
        }

this.$router.push("/")不是跳到首頁,而是變成這樣:http://127.0.0.1:8080/login?email=yejia@qq.com&password=123456 ,請問哪裡錯啦?

世界只因有你
世界只因有你

全部回覆(2)
洪涛

你這裡的 this指向已經不是 vue 的對象啦,可以這麼改

login() {
    const that = this;
    if(this.email.length > 0 && this.password.length >0) {
        this.$http.post('/api/login', {
            user: this.email,
            password: this.password
        })
        .then(res => {
            let userPwd = res.data
            if(this.password == userPwd) {
                that.$router.push("/")
            } else {
                alert("错误,请重新输入!")
            }
        })
        .catch(err => {
            console.log(err)
        })
    } else {
        alert("输入错误!")
    }
    }
世界只因有你

有沒有可能是已經跳轉了,但是主頁判斷成了沒有登錄,然後又跳回來了。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板