Why not design it so that the imported content can be directly accessed in the vue template? #9753
Replies: 1 comment 3 replies
-
Technical reasons, mostly. Let's look at an example: <script>
import uuid from 'some-uuid.-package'
export default {
data: () => ({
uuid: myUuid()
})
}
</script>
<template>
<div>{{ myUuid }}</div>
</template> The compiler transfers the template into a render function similar to this: render: (_props, ctx) => {
return h('div', ctx.myUuid)
} That is to say: all "variables" accessed in the template are rewritten to the format of The situation is a bit different when using Also, in <script>
import uuid from 'some-uuid.-package'
export default {
data: () => ({
uuid: uuid()
})
}
</script>
<template>
<!-- should this refer to the data property or the global package? -->
<div>{{ uuid }}</div>
</template> that ambiguity doe not exist in <script setup>
import uuid from 'some-uuid.-package'
// this code will immediatly throw an error because `uuid` is already defined
const uuid = ref(uuid())
</script>
<template>
<!-- So there can't be any ambiguity here -->
<div>{{ uuid }}</div>
</template> |
Beta Was this translation helpful? Give feedback.
-
Why not allow direct access to the imported data in the template? What are the considerations for this design? It feels so troublesome. It would be simpler if it could be accessed directly.
Beta Was this translation helpful? Give feedback.
All reactions