A series of useful enhancements to Vue components props:
- Transform props
- Enum-type props
- Numeric-type props
- Listen for receiving props
- Two-way data binding props
# yarn
yarn add vue-messenger
# or, npm
npm i vue-messenger --save
Available as global VueMessenger
.
// main.js
import Vue from 'vue'
import Messenger from 'vue-messenger'
Vue.mixin(Messenger)
// Component.vue
import Messenger from 'vue-messenger'
export default {
mixins: [Messenger],
// ...
}
To transform a prop, add a transform: value => transformedValue
function to its descriptor, and use this.local${PropName}
to get transformed prop. e.g.
<template>
<div>{{ normalizedMessage }}</div>
</template>
<script>
export default {
props: {
message: [Number, String]
},
computed: {
normalizedMessage() {
return String(this.message).trim().replace(/@/g, '(a)')
}
}
}
</script>
<template>
<div>{{ localMessage }}</div>
</template>
<script>
export default {
props: {
message: {
type: [Number, String],
transform: message => String(message).trim().replace(/@/g, '(a)')
}
}
}
</script>
To define a enum-type prop, add a enum
array to its descriptor, and its default
value will be enum[0]
if the descriptor doesn't contain default
attribute. e.g.
export default {
props: {
size: {
type: String,
default: 'small',
validator: value => ['small', 'large'].indexOf(value) >= 0
}
}
}
export default {
props: {
size: {
type: String,
enum: ['small', 'large']
}
}
}
To define a numeric-type prop, add numeric: true
to its descriptor. Besides, you can set infinite
to ture
to allow infinite numbers, which are -Infinity
and Infinity
. e.g.
export default {
props: {
count: {
type: [Number, String],
default: 0,
validator: value => !isNaN(value - parseFloat(value))
}
},
max: {
type: [Number, String],
default: Infinity,
validator: value => value === Infinity || !isNaN(value - parseFloat(value))
}
}
}
export default {
props: {
count: {
numeric: true,
default: 0
},
max: {
numeric: true,
infinite: true,
default: Infinity
}
}
}
To listen for receiving a prop, add on: { receive: (newValue, oldValue) => void }
object to its descriptor. e.g.
export default {
props: {
count: [Number, String]
},
watch: {
count: {
immediate: true,
handler(newCount, oldCount) {
console.log(newCount, oldCount)
}
}
}
}
export default {
props: {
count: {
type: [Number, String],
on: {
receive(newCount, oldCount) {
console.log(newCount, oldCount)
}
}
}
}
}
To apply two-way data bindings on a prop, add sync: true
to its descriptor. Then, you can use this.local${PropName} = newValue
or this.send${PropName}(newValue)
to send new value to Parent component.
If the prop is model prop, it's no need to add
sync: true
to its descriptor.
<!-- // Parent.vue -->
<template>
<Child v-model="value" :visible.sync="visible" />
</template>
<script>
import Child from './Child.vue'
export default {
components: { Child },
data: () => ({
value: String,
visible: Boolean
})
}
</script>
<!-- // Child.vue -->
<template>
<div v-show="curVisible">
<input v-model="curValue" />
</div>
</template>
<script>
export default {
props: {
value: String,
visible: Boolean
},
computed: {
curValue: {
get() {
return this.value
},
set(newValue) {
if (newValue === 'hide') {
this.curVisible = false
}
this.$emit('input', newValue)
}
},
curVisible: {
get() {
return this.visible
},
set(newVisible) {
this.$emit('update:visible', newVisible)
}
}
}
}
</script>
<!-- // Parent.vue -->
<template>
<Child v-model="value" :visible.sync="visible" />
</template>
<script>
import Child from './Child.vue'
export default {
components: { Child },
data: () => ({
value: String,
visible: Boolean
})
}
</script>
<!-- // Child.vue -->
<template>
<div v-show="localVisible">
<input v-model="localValue" />
</div>
</template>
<script>
export default {
props: {
value: {
type: String,
on: {
change(value) {
if (value === 'hide') {
this.localVisible = false
}
}
}
},
visible: {
type: Boolean,
sync: true
}
}
}
</script>