admin管理员组

文章数量:1289583

I was wondering if it's possible to have access to entire object inside if/for, but still be able to destructure individual props

{#if object_var}
  {const {prop_var} = object_var}. <- doesn't work, "unexpected token"
  
  display: {prop_var}

{/if}

I was wondering if it's possible to have access to entire object inside if/for, but still be able to destructure individual props

{#if object_var}
  {const {prop_var} = object_var}. <- doesn't work, "unexpected token"
  
  display: {prop_var}

{/if}
Share Improve this question asked Feb 20 at 18:53 AlexAlex 68.1k185 gold badges459 silver badges650 bronze badges 1
  • what token is unexpected? the dot? – Nina Scholz Commented Feb 20 at 18:58
Add a comment  | 

1 Answer 1

Reset to default 1

Without any leading symbol, the curly braces signal a text expression. You cannot use arbitrary JS there; regular declarations are not allowed.

There is a @const tag that can be used in some specific places.
Here this would work since the tag is an immediate child of #if:

{#if object_var}
  {@const { prop_var } = object_var}
  display: {prop_var}
{/if}

本文标签: javascriptDestructure and don39t destructure variable at the same time in svelteStack Overflow