【发布时间】:2016-08-17 02:06:41
【问题描述】:
在header.pug
block link
-var selected='Home';
-var menu = [{'title':'Home','address':'/home/home.html','child':[]},{'title':'Shopping','address':'/shopping/shopping.html','child':[{'title':'TV','address':'/tv/tv.html'},{'title':'Smartphone','address':'/smartphone/smartphone.html'}]},{'title':'About','address':'/about/about.html','child':[]}]
#navbar
+navbar(selected,menu)
在mixin.pug
//- Navbar mixin
mixin navbar(selected, menus)
ul
each menu in menus
if selected === menu.title
li.active
a(href=menu.address, title=menu.address)= menu.title
if menu.child.length > 0
+navbar(selected, menu.child)
else
li
a(href=menu.address, title=menu.address)= menu.title
if menu.child.length > 0
+navbar(selected, menu.child)
问题:
编译成html时出现错误,在控制台显示:
Cannot read property of undefined in the "if menu.child.length > 0" row
编辑:
添加图片:
【问题讨论】:
-
您的子菜单项没有
child数组属性。将child: []添加到每个子项中,或者调整您的 if 语句以防止缺少该属性(即if menu.child && menu.child.length