超出最大调用堆栈大小。递归标签

时间:2016-05-12 13:47:38

标签: javascript tags riot.js

我的对象如下:

<app>
  <child opts="{ json }"></child>
  <script>
    this.json = [
      {
        text: 'parent',
        child: [
          {
            text: 'child1',
            child: {
              text: 'child2'
            }
          }
        ]
      }
    ];
  </script>
</app>

每个孩子都可以拥有自己的孩子。所以我在这里需要递归标记。这就是我所拥有的:

<child>
<h1>child: { opts.opts.text }</h1>
<div if={opts.opts.child}>
    <child opts={opts.opts.child}></child>
</div>
</child>

我得到Maximum call stack size exceeded。我读到暴乱中的js递归标签是一个问题,但没有找到任何解决方案,或者说它不可能。

1 个答案:

答案 0 :(得分:0)

我在这里扩展了@Heikki的例子:http://plnkr.co/edit/12AyPoahb9vGOvx06qqv?p=preview

数据结构略有不同:

this.json = {
  text: 'parent',
  children: [
    {
      text: 'child1',
      children: [{
        text: 'child2',
        children: [
          {text: 'Inner', children: [
            {
              text: 'Inner inner',
              children: []
            },
            {
              text: 'Inner inner 2',
              children: []
            }
          ]}
        ]
      }]
    }
  ]
};

使用更新的子标记:

<child>
  <h1>Text: { this.opts.data.text }</h1>
  <virtual each={ child in this.opts.data.children }>
    <child data="{ child }"></child>
  </virtual>
</child>
相关问题