Skip to content

Instantly share code, notes, and snippets.

@syuji-higa
Last active November 16, 2019 04:01
Show Gist options
  • Save syuji-higa/9eb90ec02d1f724e896d586404623d1d to your computer and use it in GitHub Desktop.
Save syuji-higa/9eb90ec02d1f724e896d586404623d1d to your computer and use it in GitHub Desktop.
Vue.js - data flow
<template>
<Grandchild
:name="name"
@input="updateData('name', $event)"
/>
</template>
<script>
import Grandchild from '~/components/Grandchild'
export default {
components: {
Grandchild
}
props: {
name: {
type: String,
required: true
}
},
methods: {
updateData(key, val) {
this.$emit(`update:${key}`, val)
}
}
}
</script>
<template>
<input
type="text"
:value="name"
@input="onInput"
/>
</template>
<script>
export default {
props: {
name: {
type: String,
default: ''
}
},
methods: {
onInput(val) {
this.$emit('input', val)
}
}
}
</script>
<template>
<Child :name.sync="name" />
</template>
<script>
import Child from '~/components/Child'
export default {
components: {
Child
},
data() {
return {
name: '田中'
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment