1我正在尝试验证文本框。我有一个具有模糊事件的子组件。和v-if语句,如果为true则显示错误。开启模糊效果很好,我逐步执行了代码。它似乎可以满足我的要求,但是父组件/ v-if语句上的error属性没有更新(显示错误),这是我的代码:
//Child component
<template>
<input type="text"
class="form-control"
id="input-username"
name="custid"
v-on:blur="$emit('hasValidCustomerId')"
v-model="custid"/>
<div v-if="custidError"> Error! </div>
<template/>
//Parent Component
<div id="password-form">
<transition name: currentTransition>
<component
v-on:changeSlide="changeSlide"
v-on:blur="$emit('hasValidCustomerId')"
:is="slides[currentSlide]">
</component>
</transition>
</div>
我的JavaScript
// Child Component
var accountDetails =
{
template: '#template',
components: { },
data: function () {
return {
custid: '',
custidError: false,
currentSlide: 0,
currentTransition: ''
};
},
computed: {},
methods: {},
};
// Parent Component
var passwordFlow =
{
template: '#template',
components: {
"login": login,
"account-details": accountDetails,
"otc-options": otcOptions
},
data: function () {
return {
slides: ['login', 'account-details', 'otc-options'],
currentSlide: 0,
currentTransition: ' '
custid: '',
custidError: false
};
},
computed: {},
methods: {
hasValidCustomerId: function () {
if (this.custid === " ")
this.custidError = true; ///This gets set to true
console.log(this.custidError); ///this logs false ??
},
};
VUE实例
el: "#login-main",
data: {},
components: {
"passwordFlow": passwordFlow,
},
computed: {},
mounted: function () { },
methods: {}
});
答案 0 :(得分:0)
在子组件中,将custidError
从数据更改为道具
// Child Component
var accountDetails =
{
template: '#template',
components: { },
props: {
custidError: {
type: Boolean,
default: false
}
},
data: function () {
return {
custid: '',
currentSlide: 0,
currentTransition: ''
};
},
computed: {},
methods: {},
};
然后在parent中,将其传递给组件
<component :custidError="custidError"
v-on:changeSlide="changeSlide"
v-on:blur="$emit('hasValidCustomerId')"
:is="slides[currentSlide]">
</component>