代码之家  ›  专栏  ›  技术社区  ›  Billal Begueradj

Nuxt.js:了解<Nuxt child>组件

  •  0
  • Billal Begueradj  · 技术社区  · 6 年前

    在Nuxt.js中,我创建了以下文件夹结构:

    ├── parent
    │   ├── child1.vue
    │   └── child2.vue
    ├── parent.vue
    

    父.vue ,我有这个:

    <template>
        <div>
            <h3>Parent element</h3>
            <ul>
                <li><nuxt-child/></li>
                <li><nuxt-child/></li>
            </ul>
        </div>
    </template>
    

    儿童1.vue

    <template>
        <div>
            <h3>Child 1</h3>
        </div>
    </template>
    

    儿童2.vue

    <template>
        <div>
            <h3>Child 2</h3>
        </div>
    </template>
    

    我启动服务器(运行dev)并执行以下URI: http://localohost:3000/parent

    Parent element    
         - 
         -  
    

    如果我去 http://localohost:3000/parent/child1 ,我看到这个:

    Parent element    
         - Child 1
         - Child 1
    

    如果我去 http://localohost:3000/parent/child2 ,我看到这个:

    Parent element    
         - Child 2
         - Child 2
    

    问题:

    documentation ,我明白 儿童1.vue 儿童2.vue 是parent.vue的孩子,所以我希望在访问时看到他们的列表 http://localhost:3000/parent ,但它们没有显示。每个子级仅在指向其URI时显示。有人能解释我的这种行为吗?

    1 回复  |  直到 6 年前
        1
  •  13
  •   Zoe Edwards    6 年前

    <nuxt-child/> 是子组件的替代品, 基于 .

    <template>
        <div>
            <h3>Parent element</h3>
            <nuxt-child/>
        </div>
    </template>
    

    然后把你需要的东西放在孩子身上。

    nested routes .

    例如,假设我有一些事件。父页是 event ,则每个事件都是一个子事件。

    - events
        - christmas
        - easter
    

    当我去 events/christmas events/easter ,我将看到活动页面,但其中包含我想要的活动内容。父页 events/