Created
January 17, 2023 19:34
-
-
Save zach2825/fc258d5a7ae1848c2559c105d807617f to your computer and use it in GitHub Desktop.
SampleComponent vue3 vue.extend replacement
This file contains 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> | |
<div> | |
Welcome<br /> | |
<SampleVueComponent :testing-prop="'test'" /> | |
</div> | |
</template> | |
<script lang="ts"> | |
import { defineComponent, ref } from 'vue'; | |
interface Mehtods { | |
someTest: () => void; | |
} | |
interface Data { | |
message: string; | |
} | |
interface Computed { | |
preMessage: string; | |
} | |
interface Props { | |
testingProp: 'test' | 'working'; | |
} | |
function createComponent<M extends Mehtods, D extends Data, C extends Computed, P extends Props>(options: { | |
name: string; | |
data: () => D; | |
methods: M; | |
computed: C; | |
props?: P; | |
}) { | |
return defineComponent({ | |
name: options.name, | |
data() { | |
return options.data(); | |
}, | |
methods: options.methods, | |
computed: options.computed, | |
props: options.props | |
}); | |
} | |
const SampleVueComponent = createComponent<Methods, Data, Computed, Props>({ | |
name: 'SampleVueComponent', | |
data(){ | |
return { | |
message: 'World' | |
} | |
}, | |
methods: { | |
someTest(){ | |
console.log('Hello World', this.method) | |
} | |
}, | |
computed: { | |
preMessage(){ | |
return 'Hello' | |
} | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment