2016-05-12 107 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> 
相關問題