[riot.js] 如何向子Tag传递父Tag中的数据

本文探讨了在使用Riot.js框架时,如何在子组件的循环结构中正确访问父组件的属性,例如获取父级的items长度。

使用riot遇到一个问题,如以下示例:

<html>
  <head>
    <title>Riot Demo</title>
  </head>
  <body>

    <script type="riot/tag">
      <cards>
        <ul>
          <card each={opts.items} data={this} width={parent.opts.items.length}></card>
        </ul>
      </cards>

      <card>
        <li class="card">{opts.data.name}-{opts.width}</li>
      </card>
    </script>

    <cards></cards>

    <script src="bower_components/riot/riot+compiler.min.js"></script>
    <script>
      riot.mount('cards', {'items':[{name:'abc'}, {name:'def'}]})
    </script>
</html>

当我想在 card 中使用父标签的 items.length 时如何处理。这里我使用了 parent.opts.items.length ,即parent不能省。

BTW:

  • this 指当前的循环变量。
  • 循环如何指定非this的循环变量,可以 each = {item in items} ,后面可以使用 item

转载于:https://my.oschina.net/limodou/blog/680153

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值