Strange Vue behavior #6842
-
Beta Was this translation helpful? Give feedback.
Answered by
liulinboyi
Oct 10, 2022
Replies: 1 comment
-
It's a feature. The compiled location of code in <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-const",
"iRef": "setup-ref",
"test": "setup-const",
"msg": "setup-ref"
} */
import { defineComponent as _defineComponent } from 'vue'
console.log('--------noSetup---------')
import { ref } from 'vue'
const iRef = ref()
const test = 'test'
console.log('--------noSetup---------')
import { onMounted } from 'vue'
const __sfc__ = /*#__PURE__*/_defineComponent({
__name: 'App',
setup(__props, { expose }) {
expose();
console.log('--------Setup---------')
const msg = ref('Hello World!')
onMounted(() => {
console.log(iRef)
console.log(test)
})
console.log('--------Setup---------')
const __returned__ = { iRef, test, msg }
Object.defineProperty(__returned__, '__isScriptSetup', { enumerable: false, value: true })
return __returned__
}
})
import { toDisplayString as _toDisplayString, createElementVNode as _createElementVNode, vModelText as _vModelText, withDirectives as _withDirectives, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"
function render(_ctx, _cache, $props, $setup, $data, $options) {
return (_openBlock(), _createElementBlock(_Fragment, null, [
_createElementVNode("h1", null, _toDisplayString($setup.msg), 1 /* TEXT */),
_withDirectives(_createElementVNode("input", {
ref: "iRef",
"onUpdate:modelValue": _cache[0] || (_cache[0] = $event => (($setup.msg) = $event))
}, null, 512 /* NEED_PATCH */), [
[_vModelText, $setup.msg]
])
], 64 /* STABLE_FRAGMENT */))
}
__sfc__.render = render
__sfc__.__file = "App.vue"
export default __sfc__ But i don't suggest use like this. <script setup> can be used alongside normal <script>. A normal <script> may be needed in cases where we need to:
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
louiss0
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a feature. The compiled location of code in
<script>
will be above code in<script setup>
.Playground Demo
After compiled