我希望通过在RiotJSeach={}循环期间缓存重复的值来提高性能和可读性。有没有办法做到这一点,就不会产生价值和/或招致不必要的性能开销?

之前:

<my-tag>
  <p each="{item in data}">
    <b if="{item.style == 'b'}">{item.text}</b>
    <i if="{item.style == 'i'}">{item.text}</i>
  </p>
  data = [
    {"text": "bold me", "style": "b"},
    {"text": "italicize me", "style": "i"}
  ];
</my-tag>

<!-- Renders <p><b>bold me</b></p> <p><i>italicize me</i></p> -->


后:

<my-tag>
  <p each="{item in data}">
    {style=item.style}
    {text=item.text}
    <b if="{style == 'b'}">{text}</b>
    <i if="{style == 'i'}">{text}</i>
  </p>
  data = [
    {"text": "bold me", "style": "b"},
    {"text": "italicize me", "style": "i"}
  ];
</my-tag>

<!-- Renders <p>b bold me <b>bold me</b></p> <p>i italicize me <i>italicize me</i></p> -->


jsfiddle

最佳答案

我认为您想这样做:

<p each="{data}">
  <b if="{style == 'b'}">{text}</b>
  <i if="{style == 'i'}">{text}</i>
</p>


jsfiddle

关于javascript - RiotJS:如何在循环期间缓存值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38514280/

10-11 14:28