-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Allow @const inside an if block #7241
Comments
Can you show a usecase where this feature would help? |
Something like this (this is very similar to my use case): <div>
{#if selected.length == 1}
{@const sel = selected[0]}
<!-- some actions on this one -->
{:else if selected.length == 0}
none selected
{:else}
multiple selected
{/if}
</div> |
Just a possible approach for this, would the only change required be the addition of
Or is there something more missing? |
Why don't just make it free to use without limiting it to what parent it has? |
It is hard to implement the way Svelte handles blocks. In the output they get compiled to different functions which are all in the same scope, and can't access the variables of a different function. The only way this could work is by passing a separate argument like I think it will still be harder because of reactivity, but that is most likely what is to do. |
I arrived here from a searching for a related idea: Allowing "as" clauses on if, like with each. It would be nice to have something like This would be analogous to As a workaround, can do something like: A example use for this would be something like: |
You should open a separate issue for this! That is a really good idea! |
We won't be adding new syntax to the |
@dummdidumm Just so I understand, is there some stated rationale for why not to add syntax to Not to advocate too much, but it seems like the |
@brandonbloom you can extract data you need in the <script>
// some code
$: items = path?.to?.container?.items;
// more code
</script>
{#if items}
<!-- your markup -->
{/if} Avoid doing calculations and long expressions in the markup, move them to the script. |
Yeah, of course, that's what I've been doing. I've just noticed that the lack of something like |
I can certainly see the use-case here. I hope they do add support for In the meantime, if you’d like to move your logic to your templating and out of your JS reactivity, this should work (low-level workaround): {@const items = path?.to?.container?.items}
{#if items}
<!-- your markup -->
{/if} |
Your example doesn't compile, for the same reason as my original example. |
@blaumeise20 Ah I see that now. I sincerely hope they do add this soon. I don't see how this could be much different from the existing implementation, as if-statements and loops both have the same block scope-treatment in JS, unless the Svelte compiler compiles One method I think should work (just as a temporary workaround) is transforming your {#if selected.length === 1}
{#each selected as sel}
one selected
{/each}
{:else if !selected.length}
none selected
{:else}
multiple selected
{/if} This should work as expected, mainly because there is only one element in the dataset being evaluated in your if-condition. If, however there are more a single element, the same logic would apply for multi-element datasets like, if you only want to use one of the items for your {#if selected.length}
{#each [selected[0]] as sel}
any number selected, creating a const-style variable for a specific element(s) in the set
{sel} can be used here
{/each}
{:else}
none selected
{/if} This should work identically to your use case and will only iterate over, and alias, the single item you are wanting to evaluate. If you want to define multiple values as you might using destructuring within a {#if selected.length}
{#each [[selected[0], selected[2]]] as [val1, val2]}
{val1} and {val2} can both be used here, in this single-iteration loop
{/each}
{:else}
... To reiterate though, I am very much in favor of adding support for What I provided in my previous comment is merely a workaround. |
This is supported now in 3.48.0. |
Describe the problem
I want to make a
{@const a = b}
declaration inside an{#if cond}
block, but svelte complains about it's parent.Describe the proposed solution
It would be cool if it just worked.
Alternatives considered
None
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: