Skip to content

Instantly share code, notes, and snippets.

@potato4d
Created October 19, 2017 06:57
Show Gist options
  • Save potato4d/7f349deb8354603e25345d097a96fa57 to your computer and use it in GitHub Desktop.
Save potato4d/7f349deb8354603e25345d097a96fa57 to your computer and use it in GitHub Desktop.
Vue.jsのpropsカスタムバリデーターを使った堅牢なコンポーネント作成 ref: http://qiita.com/potato4d/items/1b92df0cbf9b0b6cf8d6
export const accountKeys = [
'id',
'name',
'bio'
];
import { hasKeys } from 'path/to/ObjectTypeCheck';
import { accountKeys } from './AccountInfo.type';
export default {
props: {
account: {
validator(val) {
return hasKeys(val, accountKeys);
}
}
}
}
./LikeButton.vue
./LikeButton.type.js
./AccountInfo.vue
./AccountInfo.type.js
export const size = [
'small',
'medium'
];
<template>
<div>
<button :type="type" class="like-button" :class="buttonClass" @click="handleClick">
+{{count}}likes
</button>
</div>
</template>
<style>
.like-button {
/* 何かしらのスタイル */
}
.like-button.button-small {
padding: 6px 10px;
}
.like-button.button-medium {
padding: 10px 13px;
}
</style>
<script>
export default {
props: ['count', 'size'],
data () {
return {
isClicked: false
}
}
computed: {
nowCount () {
return this.isClicked ? ( this.count + 1 ) : this.count;
},
buttonClass () {
const style = {}
style[`button-${this.size}`] = true
return style
}
},
methods: {
handleClick () {
this.isClicked = !this.isClicked;
}
}
}
</script>
export function hasKeys(obj, keys) {
return !keys.find((key)=>(!obj.hasOwnProperty(key)))
}
export default {
props: {
count: Number,
size: String
}
}
export default {
props: {
count: Number,
size: {
type: String,
validator (val) {
return ['small', 'medium'].includes(val)
}
}
}
}
export default {
props: {
count: Number,
size: {
type: String,
validator (val) {
return ['small', 'medium'].includes(val)
}
}
}
}
export default {
props: {
count: Number,
size: String
}
}
export default {
props: ['count', 'size']
}
export default {
props: {
count: {
type: Number,
required: true
},
size: {
type: String,
default: 'meduim',
validator (val) {
return ['small', 'medium'].includes(val)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment