TomatoYuyuko
V2EX  ›  问与答

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

  •  
  •   TomatoYuyuko · Sep 10, 2021 · 848 views
    This topic created in 1711 days ago, the information mentioned may be changed or developed.
    假设我现在有一个 JSON,告诉我,这个页面里有 3 种组件 ABC,但是数量不确定,可能是 AAABCCCCC 或者 BBCC 这种,每个组件的 prop 具体值都不同(但同类型组件格式相同),我该怎么设计父组件?才能保证拿到这个 json 后准确的渲染出页面?并保证随时上下通信
    4 replies    2021-09-10 15:02:49 +08:00
    murmur
        1
    murmur  
       Sep 10, 2021
    这不就是动态页面么,最简单的一个 v-for 一个 v-if,连 render 都不用写,然后用事件或者传入函数通信,传入的时候把自己的 id 带上就行了
    murmur
        2
    murmur  
       Sep 10, 2021   ❤️ 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  
       Sep 10, 2021   ❤️ 1
    动态组件 <component :is="component.name"/>
    TomatoYuyuko
        4
    TomatoYuyuko  
    OP
       Sep 10, 2021
    @murmur 啊多谢!思维僵化了,确实可以循环动态,只想到了动态,没想到循环、、、
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3474 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 04:41 · PVG 12:41 · LAX 21:41 · JFK 00:41
    ♥ Do have faith in what you're doing.