[Vuejs]-How can I access props using the Vue 3 composition API?

1👍

One way is to use setup script. This is recommended SFC approach.

  1. you do not have to reference props from template.
  2. you do not have to return props, includes, computed, etc. from script

https://vuejs.org/api/sfc-script-setup.html#typescript-only-features

<template>
  <div>
    <div>Prop: {{ msgProp }}</div>
  </div>
</template>

<script setup lang="ts">
import { defineProps, onMounted } from "vue";

interface IDemoProps {
  msgProp: string;
}

const props = defineProps<IDemoProps>();

onMounted(() => {
 console.log(props)
});
</script>

Leave a comment