Skip to content

Strange Vue behavior #6842

Closed Answered by liulinboyi
louiss0 asked this question in Help/Questions
Discussion options

You must be logged in to vote

It's a feature. The compiled location of code in <script> will be above code in <script setup>.
Playground Demo

<script lang="ts" setup>
console.log('--------Setup---------')
import { ref, onMounted } from 'vue'

const msg = ref('Hello World!')

onMounted(() => {
  console.log(iRef)
  console.log(test)
})
console.log('--------Setup---------')
</script>

<script lang="ts">
console.log('--------noSetup---------')
import { ref } from 'vue'
const iRef = ref()
const test = 'test'
console.log('--------noSetup---------')
</script>

<template>
  <h1>{{ msg }}</h1>
  <input ref="iRef" v-model="msg">
</template>

After compiled

/* Analyzed bindings: {
  "ref": "setup-const",
  "onMounted": "setup-co…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by louiss0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants