Skip to content

Instantly share code, notes, and snippets.

@syuji-higa
Last active November 16, 2019 03:58
Show Gist options
  • Save syuji-higa/80f8ec8060a080cbffebdb8b5a58c095 to your computer and use it in GitHub Desktop.
Save syuji-higa/80f8ec8060a080cbffebdb8b5a58c095 to your computer and use it in GitHub Desktop.
Vue.js - child component status
<template>
<input type="text" :value="name" @input="onInput" />
</template>
<script>
export default {
props: {
name: {
type: String,
required: true
},
// validation data
isInvalid: {
type: Boolean,
required: true
}
},
watch: {
// update validation data
name() {
this.updateIsInvalid()
}
},
created() {
// init validation data
this.updateIsInvalid()
},
methods: {
onInput(val) {
this.$emit('update:name', val)
},
updateIsInvalid() {
const isInvalid = name === ''
this.$emit('update:isInvalid', isInvalid)
}
}
}
</script>
<template>
<Child :name.sync="name" :is-invalid.sync="isInvalid" />
</template>
<script>
export default {
data() {
return {
name: '',
isInvalid: false
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment