頁面自動刷新和注冊邏輯問題
rt,代碼如下:
<template> ??<div> ????<form?v-if="!isReg"> ??????<label?for="name1">User?Name:?</label> ??????<input?type="text"?id="name1"?v-model="name"><br> ??????<label?for="pwd1">Password:?</label> ??????<input?type="password"?id="pwd1"?v-model="pwd"><br> ??????<button?@click="login()">Login</button> ??????<button?@click="reg()">register</button> ????</form> ????<form?v-else> ??????<label?for="name2">User?Name:?</label> ??????<input?type="text"?id="name2"?v-model="name"><br> ??????<label?for="pwd2">Password:?</label> ??????<input?type="password"?id="pwd2"?v-model="pwd"><br> ??????<label?for="pwda">Password?Again:?</label> ??????<input?type="password"?id="pwda"?v-model="pwda"><br> ??????<button?@click="confirm()">confirm?register</button> ??????<button?@click="cancel()">cancel?register</button> ????</form> ??</div> </template>
login()?{ ??this.$router.push('home') }, reg()?{ ??this.isReg?=?true }, cancel()?{ ??this.isReg?=?false }, confirm()?{ ??if(this.pwd?===?this.pwda?&&?this.name?!==?''?&&?this.pwd?!==?'')?{ ????localStorage.setItem('name',this.name) ????localStorage.setItem('pwd',this.pwd) ????this.name?=?'' ????this.pwd?=?'' ????this.isReg?=?false ??}?else?{ ????alert("兩次輸入不一致") ??} }
沒有報錯,問題在于點擊任意按鈕時頁面都會自動刷新,這就導致點擊注冊時,注冊表單一閃而過刷新回登錄,此時url為localhost:8080/?,我重啟服務、瀏覽器都沒有用。
還有最后這個confirm函數,空信息注冊會彈窗,但是localstorage也會存數據,相當于兩個分支都走了一遍,有點不可思議,是我判斷的條件有問題嗎?
有點懵,還請賜教。
2019-05-30
試試,button 加一個type="button"
2019-10-24
完美解決??