I'm quite stuck trying to use properties with Vue3. I've tried a few different approaches, but all of them fail the type-check phase (e.g.: yarn build).
My project is a brand new vue3-ts project created using Vite. This is my component:
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
name: "Test",
props: {
label: {
type: String as PropType<string>,
required: true,
},
},
methods: {
onClick() {
console.log(this.label); // This line yields an error!
},
},
});
</script>
I get an error that this.label does not exist: Property 'label' does not exist on type 'CreateComponentPublicInstance<Readonly<ExtractPropTypes<Readonly<ComponentPropsOptions<Data>>>> & ...
(volar complains about the same thing).
I've tried a few different approaches with no better luck, these are:
Using the <script setup> approach defining props:
<script setup lang="ts">
const props = defineProps({
classes: String,
label: String,
})
</script>
This also warns about the unused props variable. That's not a big deal, but the above error is still there.
Using a setup method on my component:
setup(props) {
defineProps({
classes: String,
label: String,
})
},
Using the old-school form of defining props, a bit overzealous with defining types:
export default defineComponent({
name: "AppStory",
props: {
label: {
type: String as PropType<string>,
required: true,
},
},
A slightly less zealous approach with the types:
export default defineComponent({
name: "AppStory",
props: {
label: {
type: String,
required: true,
},
},
Does anyone have a working example of SFC with Vue3 that use properties? What am I doing wrong? All the examples I'm finding out there have no props, or don't use TS. Vue'3 docs aren't very TS-centric, and no examples seem to cover this (rather basic) scenario.