KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
🖖

Vue

Topic Hub & Articles

Vue Intro

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Get Started

10 min

Vue Directives

10 min

Vue Events

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Forms

10 min

Vue Components

10 min

Vue Props

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Methods

10 min

Vue Computed Properties

10 min

Vue Watchers

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Class and Style Bindings

10 min

Vue Conditional Rendering

10 min

Vue List Rendering

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Template Syntax

10 min

Vue Teleport

10 min

Vue Suspense

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Transitions

10 min

Vue Mixins

10 min

Vue Custom Directives

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Plugins

10 min

Vue Slots

10 min

Vue Provide / Inject

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Dynamic Components

10 min

Vue Async Components

10 min

Composition API Intro

10 min

Recap Quiz

Gated

Milestone Assessment

ref vs reactive

10 min

Lifecycle Hooks

10 min

Dependency Injection

10 min

Recap Quiz

Gated

Milestone Assessment

Computed and Watch

10 min

Template Refs

10 min

Composable Functions

10 min

Script Setup

10 min

Progress
0%

0 / 32 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
VueVue Composition API
Lesson

Script Setup

10 min reading
Free Course

Script Setup: Modern SFC Ergonomics and Compiler Macros

<script setup> is Vue 3's compile-time syntax for using the Composition API inside Single File Components (SFCs). Code inside <script setup> compiles directly into the component's setup() function, removing boilerplate and improving runtime performance.

Every top-level variable, function, and imported component inside <script setup> becomes available in <template> automatically. You no longer need to list components manually or write a giant return { ... } object at the end of the file.

flowchart LR
    SFC["script setup + template"] --> Compiler["Vue SFC Compiler"]
    Compiler --> Output["Inlined setup() Render Closure"]

Built-in Compiler Macros in <script setup>

Vue provides special compile-time macros that work inside <script setup> without needing import statements:

  • defineProps(): Declares incoming component props with runtime or TypeScript validation.
  • defineEmits(): Declares custom events that the component can emit to its parent.
  • defineExpose(): Chooses which methods or refs a parent template ref can access.
  • defineModel(): Simplifies two-way v-model bindings on custom components (Vue 3.4+).
  • withDefaults(): Supplies default values when using TypeScript type-based defineProps<Props>().

Practical SFC <script setup> Example

This counter component combines defineProps, defineEmits, reactive state, and an imported child component in a compact .vue file.

<script setup>
import { ref, computed } from 'vue';
import UserBadge from './UserBadge.vue';

const props = defineProps({
  initialCount: {
    type: Number,
    default: 0
  }
});

const emit = defineEmits(['update:count', 'reset']);

const count = ref(props.initialCount);
const isPositive = computed(() => count.value > 0);

function handleIncrement() {
  count.value++;
  emit('update:count', count.value);
}

function handleReset() {
  count.value = 0;
  emit('reset');
}
</script>

<template>
  <div class="setup-card">
    <UserBadge name="Senior Developer" />
    <p>Counter: {{ count }} (Positive: {{ isPositive }})</p>

    <button @click="handleIncrement">Increment</button>
    <button @click="handleReset">Reset</button>
  </div>
</template>

Common Mistakes

  • Importing defineProps or defineEmits: Writing import { defineProps } from 'vue' is unnecessary and triggers compiler warnings. Compiler macros are globally recognized inside <script setup>.
  • Calling Macros Inside Conditional if Blocks: Compiler macros like defineProps and defineEmits must stay at the top level of <script setup> and cannot reference local variables declared inside the component.
  • Using Top-Level await Without <Suspense>: Writing top-level await inside <script setup> turns the component into an async dependency that requires a parent <Suspense> boundary to render.

Frequently Asked Questions

How do I set component options like name or inheritAttrs: false in <script setup>?

In Vue 3.3+, call the built-in defineOptions({ name: 'CustomCard', inheritAttrs: false }) macro directly inside <script setup>.

Why does <script setup> run faster than standard setup()?

Because <script setup> compiles the template inside the same function scope as the script, compiled render code accesses variables directly without going through an extra proxy object.

Can I pair <script setup> with a normal <script> block?

Yes. If you need to declare module-level exports or run side effects once per module rather than once per component instance, include a standard <script> block alongside <script setup>.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Related Developer Tools & Topic Hub

Vue Topic Hub
JSON Formatter
Validate and format JSON payloads
HTML Formatter
Format HTML templates
CSS Formatter
Clean and format stylesheets
All Vue Articles
Deep-dive guides, tips & discussions
PreviousComposable Functions
Continue JourneyComplete Topic

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum