The Anatomy of a Vue 3 Component

<p>V</p> <p>ue 3 has made significant progress and reached a strong level of maturity with Composition API &amp; Script Setup being the recommended and widely adopted syntax for writing our components. This differs from the options API, which was the preferred approach in Vue 2.</p> <p>Let&#39;s explore together all the ingredients of a Vue 3 component using Composition API and Script Setup.</p> <p><strong>tl;dr&nbsp;</strong></p> <pre> &lt;script setup&gt; import { ref, reactive, defineAsyncComponent, computed, watch, onMounted, } from &quot;vue&quot;; import useComposable from &quot;./composables/useComposable.js&quot;; import TheComponent from &quot;./components/TheComponent.vue&quot;; const AsyncComponent = defineAsyncComponent(() =&gt; import(&quot;./components/AsyncComponent.vue&quot;) ); console.log(&quot;Equivalent to created hook&quot;); onMounted(() =&gt; { console.log(&quot;Mounted hook called&quot;); }); const enabled = ref(true); const data = reactive({ variable: false }); const props = defineProps({ elements: Array, counter: { type: Number, default: 0, }, }); const { composableData, composableMethod } = useComposable(); const isEmpty = computed(() =&gt; { return props.counter === 0; }); watch(props.counter, () =&gt; { console.log(&quot;Counter value changed&quot;); }); const emit = defineEmits([&quot;event-name&quot;]); function emitEvent() { emit(&quot;event-name&quot;); } function getParam(param) { return param; } &lt;/script&gt; &lt;template&gt; &lt;div class=&quot;wrapper&quot;&gt; &lt;TheComponent /&gt; &lt;AsyncComponent v-if=&quot;data.variable&quot; /&gt; &lt;div class=&quot;static-class-name&quot; :class=&quot;{ &#39;dynamic-class-name&#39;: data.variable }&quot; &gt; Dynamic attributes example &lt;/div&gt; &lt;button @click=&quot;emitEvent&quot;&gt;Emit event&lt;/button&gt; &lt;/div&gt; &lt;/template&gt; &lt;style scoped&gt; .wrapper { font-size: 20px; } &lt;/style&gt;</pre> <h1>Script Setup</h1> <p>A Single File Component still consists of 3 parts. The template, the styles and the script. The two former are almost identical between vue 2 and vue 3, with the latter being completely revamped to the so-called script setup syntax. Declararion is simply adding the setup keyword in the script tag.</p> <p><a href="https://fadamakis.com/the-anatomy-of-a-vue-3-component-285eadadea89">Website</a></p>