最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Nuxt.js: understanding <nuxt-child> component - Stack Overflow

programmeradmin2浏览0评论

In Nuxt.js, I did this folder structure:

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

In parent.vue, I have this:

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

In child1.vue:

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

In child2.vue:

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

I launch the server (yarn run dev) and go this URI: http://localohost:3000/parent where I see this:

Parent element    
     - 
     -  

If I go to http://localohost:3000/parent/child1, I see this:

Parent element    
     - Child 1
     - Child 1

If I go to http://localohost:3000/parent/child2, I see this:

Parent element    
     - Child 2
     - Child 2

Question:

From the documentation, I understand that child1.vue and child2.vue are children of parent.vue, so I expect to see them list when I visit http://localhost:3000/parent, but they were not displayed. Each child is displayed only when I point to its URI. Anyone to explain me this behavior?

In Nuxt.js, I did this folder structure:

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

In parent.vue, I have this:

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

In child1.vue:

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

In child2.vue:

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

I launch the server (yarn run dev) and go this URI: http://localohost:3000/parent where I see this:

Parent element    
     - 
     -  

If I go to http://localohost:3000/parent/child1, I see this:

Parent element    
     - Child 1
     - Child 1

If I go to http://localohost:3000/parent/child2, I see this:

Parent element    
     - Child 2
     - Child 2

Question:

From the documentation, I understand that child1.vue and child2.vue are children of parent.vue, so I expect to see them list when I visit http://localhost:3000/parent, but they were not displayed. Each child is displayed only when I point to its URI. Anyone to explain me this behavior?

Share Improve this question asked Oct 4, 2018 at 14:29 Billal BEGUERADJBillal BEGUERADJ 22.8k45 gold badges122 silver badges140 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 17

The <nuxt-child/> is a replacement for the child component, based on the route.

Using one is all that you need:

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

Then put what you need that differs in the child.

Edit: the child page is brought in based on the route. This is a manual way of doing nested routes.

For example, say I had some events. The parent page is event, and then each event is a child.

- events
    - christmas
    - easter

When I go to events/christmas or events/easter, I’ll see the ‘event’ page but with the contents of the event I wanted. The parent page events/ could possibly just contain a list of all the events for me to visit.

发布评论

评论列表(0)

  1. 暂无评论