vue.js优先观察者不从事表单提交
我的页面正在显示用户数据,他可以编辑。 我正在处理一个小型项目。我想用Regex进行表格验证,但什么也没有发生。当我写一封不尊重正则语法的电子邮件时,没有显示任何消息。 当正则有效时,验证消息也不会出现。
export default {
name: "profile",
data() {
return {
user: {},
firstname: "",
lastname: "",
email: "",
msg: [],
};
},
watch: {
email() {
this.validateEmail(this.email);
},
},
methods: {
getProfilUser() {
UsersDataService.getUser()
.then((response) => {
let token = localStorage.getItem("token");
let decoded = VueJwtDecode.decode(token);
this.user = decoded;
console.log(response);
})
.catch((error) => {
console.log(error, "error from decoding token");
});
},
validateEmail(email) {
if (/^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/.test(email)) {
this.msg["email"] = "Email valid";
} else {
this.msg["email"] = "Adress no valid";
}
},
},
mounted() {
this.getProfilUser();
this.email;
},
};
</script>
<form class="background-style">
<div class="form-group">
<label class="form-label">Prénom</label>
<input
id="firstname"
type="text"
class="form-control"
v-model="user.firstname"
/>
</div>
<div class="form-group">
<label class="form-label">Nom</label>
<input
id="lastname"
type="text"
class="form-control"
v-model="user.lastname"
/>
</div>
<div class="form-group">
<label for="email" class="form-label">Email</label>
<input
id="email"
type="email"
class="form-control"
v-model="user.email"
/>
<span v-if="msg.email">{{ msg.email }}</span>
</div>
<div class="btn rounded p-1">
<button
type="button"
class="rounded p-2"
@click.prevent="updateProfil"
>
register
</button>
</div>
</form>
My page is displaying user data, which he can edit.
I'm working with views, on a small project. I want to do form validation with regex, but nothing happens. Exemple, When I write an email that does not respect the syntax of a regex no message is displayed.
When the regex is valid the validation message also does not appear.
export default {
name: "profile",
data() {
return {
user: {},
firstname: "",
lastname: "",
email: "",
msg: [],
};
},
watch: {
email() {
this.validateEmail(this.email);
},
},
methods: {
getProfilUser() {
UsersDataService.getUser()
.then((response) => {
let token = localStorage.getItem("token");
let decoded = VueJwtDecode.decode(token);
this.user = decoded;
console.log(response);
})
.catch((error) => {
console.log(error, "error from decoding token");
});
},
validateEmail(email) {
if (/^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/.test(email)) {
this.msg["email"] = "Email valid";
} else {
this.msg["email"] = "Adress no valid";
}
},
},
mounted() {
this.getProfilUser();
this.email;
},
};
</script>
<form class="background-style">
<div class="form-group">
<label class="form-label">Prénom</label>
<input
id="firstname"
type="text"
class="form-control"
v-model="user.firstname"
/>
</div>
<div class="form-group">
<label class="form-label">Nom</label>
<input
id="lastname"
type="text"
class="form-control"
v-model="user.lastname"
/>
</div>
<div class="form-group">
<label for="email" class="form-label">Email</label>
<input
id="email"
type="email"
class="form-control"
v-model="user.email"
/>
<span v-if="msg.email">{{ msg.email }}</span>
</div>
<div class="btn rounded p-1">
<button
type="button"
class="rounded p-2"
@click.prevent="updateProfil"
>
register
</button>
</div>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两个电子邮件属性电子邮件和user.email.you仅观看电子邮件。但是绑定用户。因此,观察者不会打电话。您可以在观察者中进行委托。以检查该
解决方案:
另一件事是直接在观察者上直接使用“ this.email”或“ this.user.email”。相反,如本代码所示,请使用newval。 VUE JS不适合更新所有数据,因此不能保证更新值,因此请务必为此使用NewVal。
There are two email property email and user.email.You are watching only email. but binding user.email in v-modal. so watchers will not call. you can console.log in watchers to check that
solutions:
And the other thing is don't use 'this.email' or 'this.user.email' directly on watchers. instead, use newVal as shown in this code. Vue js update all data properly asynchronously so which is not guaranteed for updated value so always use newVal for that.
您忘记了大约2条绑定。
首先,您应该将
V-Model
传递给您的输入。不要通过value
,v-model
将为您完成。其次,不要在Watcher中突变电子邮件 - 您将进入循环。
只需调用
validateMail
方法。那应该起作用。
You forgot about 2-way binding.
First, you should pass
v-model
to your inputs like this. Don't passvalue
,v-model
will do it for you.Second, don't mutate email inside watcher - you'll get into the loop.
Just call
validateEmail
method.That should work then.