2016-05-12 78 views
0

我的目标是这样的:超出最大调用堆栈大小。递归标签

<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递归标签是一个问题,但没有找到任何解决方案,或者它不能。

+0

你能添加代码为'小孩'标签?或者,如果是这样,那就说明你是如何使用它的。 – Heikki

+0

现在就是这样。我想在这里使用递归,因为我不知道我会有多少嵌套的孩子。 – Michal

+0

我不得不猜测,因为你的例子不完整。这是你有什么? - > http://plnkr.co/edit/07au1eYZOSjU1vldColC?p=preview – Heikki

回答

0

我已经扩展@科瓦莱宁的例子在这里: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> 
相关问题