Created
September 26, 2016 16:30
-
-
Save HerringtonDarkholme/91bb004e6aa5b545f0893fdc2d8c4538 to your computer and use it in GitHub Desktop.
high order component template in Vue2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<p> | |
<haha> | |
<v-template inline-template> | |
<span>{{$ctx.name}}说: 闷声发大财 +{{$ctx.i}}s<br/></span> | |
</v-template> | |
</haha> | |
</p> | |
</template> | |
<script> | |
import haha from './haha.vue' | |
export default { | |
components: { haha, } | |
} | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<p> | |
<button @click="repeat++">+1s</button> | |
{{repeat}}<br/> | |
<v-outlet source="ss" :$ctx="{name: '江', i: repeat}" v-for="i in repeat"> | |
<span>{{i}}颗赛艇! 总共{{repeat}}颗赛艇<br/></span> | |
</v-outlet> | |
</p> | |
</template> | |
<script> | |
import { Component, Vue, } from 'av-ts' | |
@Component | |
export default class Haha extends Vue { | |
repeat = 3 | |
} | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vue.component('v-template', { | |
created() { | |
this.$options.render = h => h('') | |
} | |
}) | |
Vue.component('v-outlet', { | |
props: { | |
source: { | |
type: String, | |
default: 'default', | |
}, | |
$ctx: {} | |
}, | |
created() { | |
let slots = this && this.$parent && this.$parent.$slots | |
let vnodes = slots && slots[this['source']] | |
let vnode = vnodes && vnodes[0] | |
let template = vnode && vnode.data && vnode.data.inlineTemplate | |
if (!template) { | |
this.$options.render = function(h) { | |
let fallbacks = this.$slots['default'] | |
return (fallbacks && fallbacks[0]) || h('') | |
} | |
return | |
} | |
this.$options.render = template.render as any | |
this.$options.staticRenderFns = template.staticRenderFns as any | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment