Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose variables in regular script block to template when using script setup #4369

Closed
henribru opened this issue Aug 17, 2021 · 5 comments
Closed
Labels
has workaround A workaround has been found to avoid the problem scope: script-setup scope: sfc ✨ feature request New feature or request

Comments

@henribru
Copy link

What problem does this feature solve?

When using script setup, you might want to define a variable in a regular script block and then use it in the template. Some examples where this are useful are when initializing the variable requires an expensive calculation or you want to share some kind of state between multiple instances of the same component (e.g. a counter for unique IDs). Currently this is a bit awkward because you have to initialize it in the regular script block and then alias it in the script setup block to be able to access it in the template.

What does the proposed API look like?

Variables in regular script blocks are automatically exposed to the template when combined with a script setup block.

@posva
Copy link
Member

posva commented Aug 17, 2021

I'm not sure this is safe to do this automatically without creating a breaking change. Having to alias it seems like a very reasonable way to do it since having expensive operations inside of an SFC is not something that happens that often:

<template>
  <h1>{{ msg }} {{ name }}</h1>
</template>

<script>
  const _name = 'my name'
</script>

<script setup>
// aliasing here
const name = _name
const msg = 'Hello World!' + name
</script>

@posva posva added the has workaround A workaround has been found to avoid the problem label Aug 17, 2021
@yyx990803
Copy link
Member

I think this is reasonable since it aligns with the compilation mental model and we already expose imports from normal <script> blocks as well.

@sqal
Copy link
Contributor

sqal commented Sep 13, 2021

@henribru I meet the same issue, especially the first one with exported variable. Can we re-open this issue co it won't be overlooked?

@henribru
Copy link
Author

I'm not able to reopen the issue but I made a new issue specifically for that first problem: #4600

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has workaround A workaround has been found to avoid the problem scope: script-setup scope: sfc ✨ feature request New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants
@yyx990803 @posva @sqal @henribru and others