【发布时间】:2018-07-18 09:56:35
【问题描述】:
我正在根据Svelte Guide for if blocks 制作一个if 块。看起来很简单,但 Svelte 认为是语法错误:
[!] (svelte plugin) ParseError: Unexpected character '#'
public\js\templates\works.html
3: <div class="slides js_slides">
4: {#each works as work, index}
5: <div class="js_slide {#if index === currentIndex }selected{/if} {#if index === 0 }first{/if}">
^
6: <img src="/images/work/screenshots/{ works[index].slug }-0.{ works[index].imageExtension }"/>
7: </div>
为什么{#if index === currentIndex } 无效?如何在 Svelte 中执行条件语句?
我不能为每个可能的结果创建单独的class= 块,但这是大量的工作。
【问题讨论】:
-
块(
{#if...、{#each...等)不能在属性中使用。相反,约定是使用三元表达式({index === currentIndex ? 'selected' : ''} {index === 0 ? 'first' : ''},或使用助手(例如class={getClass(work, index, currentIndex)})。有些人更喜欢使用data-selected={index === currentIndex} data=first={index === 0} -
谢谢 — 已将我的评论移至答案
标签: javascript css svelte