V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
TomatoYuyuko
V2EX  ›  问与答

彦祖们!请教一个 Vue 组件的问题

  •  
  •   TomatoYuyuko · 2021-09-10 14:43:16 +08:00 · 490 次点击
    这是一个创建于 960 天前的主题,其中的信息可能已经有所发展或是发生改变。
    假设我现在有一个 JSON,告诉我,这个页面里有 3 种组件 ABC,但是数量不确定,可能是 AAABCCCCC 或者 BBCC 这种,每个组件的 prop 具体值都不同(但同类型组件格式相同),我该怎么设计父组件?才能保证拿到这个 json 后准确的渲染出页面?并保证随时上下通信
    4 条回复    2021-09-10 15:02:49 +08:00
    murmur
        1
    murmur  
       2021-09-10 14:51:15 +08:00
    这不就是动态页面么,最简单的一个 v-for 一个 v-if,连 render 都不用写,然后用事件或者传入函数通信,传入的时候把自己的 id 带上就行了
    murmur
        2
    murmur  
       2021-09-10 14:52:50 +08:00   ❤️ 1
    <template v-for="component in components">
    <template v-if="component.type == 'A'">
    <A :options="component.option"></A>
    </template>
    <template v-if="component.type == 'B'">
    <B :options="component.option"></B>
    </template>
    ....
    大概就这个样
    wwwtarzan
        3
    wwwtarzan  
       2021-09-10 14:56:31 +08:00   ❤️ 1
    动态组件 <component :is="component.name"/>
    TomatoYuyuko
        4
    TomatoYuyuko  
    OP
       2021-09-10 15:02:49 +08:00
    @murmur 啊多谢!思维僵化了,确实可以循环动态,只想到了动态,没想到循环、、、
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   871 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 20:15 · PVG 04:15 · LAX 13:15 · JFK 16:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.