vue v-for 循环嵌套报错问题,请大佬闷帮忙看看 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
edk24
V2EX    Vue.js

vue v-for 循环嵌套报错问题,请大佬闷帮忙看看

  •  
  •   edk24
    edk24 2021-06-03 12:33:07 +08:00 2826 次点击
    这是一个创建于 1591 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我准备实现一个树形结构渲染,大概是以下这样

    楼栋 i 楼层 n 房间 x 

    但是在循环楼层 n房间 x时出现了报错,取不到上一个v-foritem,告诉我未定义

    我直接贴代码吧, 用的vue.js

    错误

    ReferenceError: hall is not defined 

    js

    let app = new Vue({ el: '#app', data: { // 楼栋、楼层、房间数据 hallList: [ { index: 4, name: '四号楼', show: true, floorList: [ { index: 1, name: '一层', show: true, roomList: [ { id: 1, room_number: '4604', room_type: '14', room_status: 1, is_dirty: 52, order_id: 1, member: ['余小波', '李家平'] } ] }, { index: 2, name: '二层', show: true, roomList: [ { id: 1, room_number: '4604', room_type: '14', room_status: 1, is_dirty: 52, order_id: 1, member: ['余小波', '李家平'] } ] } ] }, { index: 3, name: '三号楼', show: true, floorList: [ { index: 1, name: '一层', show: true, roomList: [ { id: 1, room_number: '4604', room_type: '14', room_status: 1, is_dirty: 52, order_id: 1, member: ['余小波', '李家平'] } ] }, { index: 2, name: '二层', show: true, roomList: [ { id: 1, room_number: '4604', room_type: '14', room_status: 1, is_dirty: 52, order_id: 1, member: ['余小波', '李家平'] } ] }, { index: 3, name: '三层', show: true, roomList: [ { id: 1, room_number: '4604', room_type: '14', room_status: 1, is_dirty: 52, order_id: 1, member: ['余小波', '李家平'] } ] } ] } ], // 当前渲染楼层 floorList: [], // 选中楼栋楼层 currentHallIndex: 3, currentFloorIndex: 1, }, }); 

    html

    <li class="hall-body" v-for="(hall, i) in hallList" :key="i"> <div class="hall-body-title"> {{hall.name}} </div> <div class="hall-body-content"> {{hall.floorList}} <!-- 楼层 --> <li class="floor-body" v-for="(floor, n) in hall.floorList" :key="n" > <div class="floor-body-title"> {{floor.name}} </div> <div class="floor-body-content"> <!-- 房间 --> <li class="room" v-for="(room, x) in floor.roomList" :key="x" > <div class="room-number">{{room.room_number}}</div> </li> </div> </li> </div> </li> 
    9 条回复    2021-06-03 14:59:02 +08:00
    hazardous
        1
    hazardous  
       2021-06-03 13:27:04 +08:00
    li 外要有 ul/ol 包裹
    hazardous
        2
    hazardous  
       2021-06-03 13:30:46 +08:00
    非 vue 问题,li 直接嵌套的话,会被 html 解析器处理成同一层级。
    PinkRabbit
        3
    PinkRabbit  
       2021-06-03 13:48:52 +08:00
    如楼上所说,li 外面套 ul/li 或者把 li 改成 div
    ooxiaoming
        4
    ooxiaoming  
       2021-06-03 14:02:44 +08:00
    @PinkRabbit 刚试了下,本地没报错,codepen 上报错了,感觉两个的 html 解析器不相同导致的
    edk24
        5
    edk24  
    OP
       2021-06-03 14:07:41 +08:00
    @PinkRabbit
    @hazardous

    感谢,没发现还造成这种问题。受教了

    要不然我今天还不如洗了蒜了
    edk24
        6
    edk24  
    OP
       2021-06-03 14:09:39 +08:00
    @ooxiaoming 这种方式我好像以前也用没啥问题, 会不会是 vue 渲染时才出现的

    li 外层用 div
    ooxiaoming
        7
    ooxiaoming  
       2021-06-03 14:14:46 +08:00
    @edk24 可能版本不一样吧,htmlParser 解析成 AST 的结果不相同
    edk24
        8
    edk24  
    OP
       2021-06-03 14:15:09 +08:00
    @edk24 确实是 html 解析的缘故,谨记教诲了 再也不偷懒在 li 外面用 div 了
    lovecy
        9
    lovecy  
       2021-06-03 14:59:02 +08:00
    @edk24 如果你不按照标准的 HTML 写,不同浏览器解析结果就会不一样,比如 chrome 会自动给你补上一些关闭 tag,会自动在 tr 外层加 tbody,但不是所有浏览器都一致
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1872 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 34ms UTC 00:01 PVG 08:01 LAX 17:01 JFK 20:01
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86