我尝试在我的 vue 项目中创建可重用的组件。这是我正在接受的培训的一部分。但我认为我的代码需要一些帮助,这让我感到困惑。
let validations = {}
validations.firstName = function(e, that) {
if (e.target.value == "") that.errors = {
[that.labelID]: 'Please enter your first name'
}
else return true
that.input_error = !that.input_error
}
validations.phone = function(e, that) {
if (e.target.value == "") that.errors = {
[that.labelID]: 'Please enter phone number'
}
else if (e.target.value.length > 10) that.errors = {
[that.labelID]: 'Phone number should be 10 digits only'
}
else return true
that.input_error = !that.input_error
}
validations.email = function(e, that) {
if (e.target.value == "") that.errors = {
[that.labelID]: 'Please enter email'
}
else return true
that.input_error = !that.input_error
}
Vue.component('childInput', {
template: '#my-child-input',
data() {
return {
errors: {},
input_error: false
}
},
props: {
labelID: String,
label: String,
inputType: {
type: String,
default: 'text'
},
value: {
type: [String, Boolean, Number],
default: null
},
},
methods: {
handleInput(e) {
this.$emit("input", e.target.value)
},
handleFocusIn(e) {
this.errors = {[this.labelID]: ''}
if (this.input_error) this.input_error = !this.input_error
},
handleFocusOut(e) {
switch (this.labelID) {
case 'firstName':
case 'phone':
case 'email':
validations[this.labelID](e, this)
break;
default:
console.log('in default last name')
break;
}
}
}
});
new Vue({
el: '#app',
data() {
return {
event: {
firstName: '',
phone: '',
email: ''
}
};
},
methods: {
handleSubmit(e) {
// I can access firstName, phone and email.
// But how to access the validation functions written in child component
console.log('All data: ', this.event)
}
}
})
.someStyleClass {
margin-bottom: 20px;
}
.input_error {
border-color: red !important;
color: red;
}
.labelStyle {
display: block;
}
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<!-- Parent Component -->
<div id="app">
<div class="someStyleClass">
<child-input v-model="event.firstName" label="First Name" label-i-d="firstName" />
</div>
<div class="someStyleClass">
<child-input v-model="event.phone" label="Phone" label-i-d="phone" />
</div>
<div class="someStyleClass">
<child-input v-model="event.email" label="* Email" label-i-d="email" input-type="email" />
</div>
<button type="submit" v-on:click="handleSubmit">Validate & Submit</button>
</div>
<!-- Child Component -->
<template id="my-child-input">
<div>
<label class="labelStyle" :class="{input_error}">{{ label }}</label>
<input :class="{input_error}" :value="value" :type="[inputType]" v-on:input="handleInput" v-on:focusout="handleFocusOut" v-on:focusin="handleFocusIn"/>
<div v-if="errors[labelID]" :class="{input_error}">{{errors[labelID]}}</div>
</div>
</template>
我能够在子级别验证输入字段,并在焦点事件发生时在输入字段附近显示其相关错误。我还可以在父组件中访问名字、电话和电子邮件。但是当我从父级提交按钮时,我无法验证这些字段。由于验证是在子级别进行的,因此错误也是如此。
单击按钮后如何检查验证并确保名字、电话和电子邮件有效。
更新 每次当用户在输入字段中输入错误数据时,都会在子级别单独验证数据。但我没有找到在父组件中找到相同内容的方法。因为我的父组件的数据变量在输入时同时更新。
点击提交后如何在父级别进行验证,无需引用且无需使用其他库?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
也许你需要的是
ref来访问子组件,这里是官方文档:https://v2.vuejs.org/v2/api/#ref对于所讨论的示例,引用用法是: