How can nested components be rendered in Vue.js? Let's explore this concept with a straightforward example

I am completely lost. I am struggling to figure out how to properly register and render a component nested within another component. Can someone please run this example, click on the About link, and take a look at the console? There seems to be a warning about component registering.

var appLayout = {
  template: `
    <div id="app" class="container">
      <header>
        <slot name="header"></slot>
      </header>

      <slot></slot>

      <footer>
        <slot name="footer"></slot>
      </footer>
    </div>
  `
}

var home = {
  template: `
    <main>
      <h3>Home</h3>
    </main>
  `
}

var about = {
  template: `
    <main>
      <nested-component></nested-component>
    </main>
  `
}

var nestedComponent = {
  template: `
    <main>
      <h3>About</h3>
    </main
  `
}

var routes = [
  {
    path: '/',
    component: home
  },
  {
    path: '/about',
    component: about
  }
]

var router = new VueRouter({
  routes
})

new Vue({
  template: '#app',
  router,
  components: {
    appLayout
  }
}).$mount('#app')
.fade-enter-active,
.fade-leave-active {
  transition-property: opacity;
  transition-duration: 0.4s;
}
.fade-enter-active {
  transition-delay: 0.2s;
}
.fade-enter,
.fade-leave-active {
  opacity: 0;
}
<template id="app">
  <app-layout>
    <nav slot="header">
      <router-link to="/">Home</router-link>
      <router-link to="/about">About</router-link>
    </nav>

    <transition name="fade">
      <router-view></router-view>
    </transition>

    <p slot="footer">
      Copyright notice
    </p>
  </app-layout>
</template>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.1/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-router/2.7.0/vue-router.min.js"></script>

Answer №1

When utilizing a component without globally registering it, you must explicitly list that component as one you will use.

var about = {
  template: `
    <main>
      <nested-component></nested-component>
    </main>
  `,
  components:{
    nestedComponent
  }
}

There are two minor bugs in the code; nestedComponent needs to be defined before about, and there was a missing angle bracket at the end of your nestedComponent.

var appLayout = {
  template: `
    <div id="app" class="container">
      <header>
        <slot name="header"></slot>
      </header>

      <slot></slot>

      <footer>
        <slot name="footer"></slot>
      </footer>
    </div>
  `
}

var home = {
  template: `
    <main>
      <h3>Home</h3>
    </main>
  `
}

var nestedComponent = {
  template: `
    <main>
      <h3>About</h3>
    </main>
  `
}

var about = {
  template: `
    <main>
      <nested-component></nested-component>
    </main>
  `,
  components:{
    nestedComponent
  }
}



var routes = [
  {
    path: '/',
    component: home
  },
  {
    path: '/about',
    component: about
  }
]

var router = new VueRouter({
  routes
})

new Vue({
  template: '#app',
  router,
  components: {
    appLayout
  }
}).$mount('#app')
.fade-enter-active,
.fade-leave-active {
  transition-property: opacity;
  transition-duration: 0.4s;
}
.fade-enter-active {
  transition-delay: 0.2s;
}
.fade-enter,
.fade-leave-active {
  opacity: 0;
}
<template id="app">
  <app-layout>
    <nav slot="header">
      <router-link to="/">Home</router-link>
      <router-link to="/about">About</router-link>
    </nav>

    <transition name="fade">
      <router-view></router-view>
    </transition>

    <p slot="footer">
      Copyright notice
    </p>
  </app-layout>
</template>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.1/vue.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue-router/2.7.0/vue-router.min.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Restrict AJAX requests to only authorized users

Simply put: In my web app that uses Javascript and PHP, there's a button that gives users 1 point when clicked. This point is then saved in a database using jQuery AJAX and PHP. However, users can bypass clicking the button by calling the script that ...

What happens if you try to add a member to a Mailchimp list who is already on the list

After following Angela Yu's course for the past few weeks, I attempted to implement the Mailchimp API as she demonstrates. However, I encountered difficulties due to recent changes in Mailchimp. Despite this setback, I was able to find the API referen ...

What are some ways to troubleshoot the UI of a Nativescript app?

As a newcomer to NativeScript technology, I often encounter challenges while developing applications. Whether it's troubleshooting why a textview is not displaying properly, identifying layout overlaps, or detecting other distortions in the UI, debugg ...

The process of combining identical data values within an array of objects

Can anyone help me with merging arrays in JavaScript? Here is an example array: [{ "team": team1, "groupname": "group1", "emp-data": [{ "id": 1, "name": "name1", }], }, { "team": team1, "groupname": "group1", " ...

What is the process for including a new path in webpack configuration?

After setting up a project using vue-cli with the command vue init webpack myProject, I found that several files and folders were created within the myProject folder. To run the project, I used the command npm run dev. Now, I am looking to expand my proje ...

Inject HTML into a div element without altering the surrounding content of the page

After observing numerous individuals inquiring about how to load HTML into a div, I managed to come up with code that accomplishes this task flawlessly. However, there is a peculiar issue where the remaining content on the page undergoes changes once the H ...

Using jQuery and Ajax to retrieve data from a PHP script's response

As a novice in jquery and Ajax functions, I am working on a website that displays data from a database using PHP. When the edit button is clicked, I want to show the information for each field. I have learned that using ajax is the best approach for this t ...

Manipulate vertices or faces within BufferGeometry using Three.js - Extrude specific elements

After creating a new THREE.PlaneBufferGeometry(100, 100, 100, 100);, I successfully updated the position of vertices to alter the shape of the mesh as shown below: https://i.sstatic.net/87BMP.png This modification was inspired by the discussion found her ...

"Troubleshooting VueJS: Issue with default value not being set in

Trying to set a default value for a select in Vue. <script src="https://unpkg.com/vue"></script> <div id="app"> <select v:model="select"> <option value="1">1</option> <option value="2">2</optio ...

How to implement automatic page refreshing in React Native using Class Components

How can I automatically refresh the order screen to fetch new data from the API without having to restart the app? ...

Tips for increasing visibility for your Google Analytics Embed API Custom Components

I recently tried to incorporate some code I found at the following link: After downloading the files view-selector2 and date-range-selector, I saved them in my local directory. I made a modification to the code: var accountSummaries = require(['&ap ...

Angular: The dilemma of choosing between updating individual properties or reassigning objects

I am currently working with an object structure that looks like this: steps:any = [ { id: 1, name: "A", next: [{ id: 2, name: "B" }, { id: 3, name: "C" }] }, { id: 2, name: "B", next: [{ id: 1, name: "B" }] }, { ...

What are some strategies for reducing the data transmitted by clients over a websocket connection?

Currently, I am utilizing the ws module and I have a need to restrict the data sent by clients over websocket to 1Mb. By setting this limit, it will deter any potential malicious users from inundating the server with large amounts of data (GB scale), poten ...

Why is the jQuery .when method not properly handling wait times?

Looking at this code I have: function checker() { return $.ajax({ type: 'POST', url: '/test/check' }) } function is_checker_true(object) { $.when(object).done(function(me) { if (!me.chec ...

Utilizing a variable in one function within a separate function (Guide to Calculating Combinations)

As a beginner in JavaScript, I'm struggling with using a variable from one function in another function. My current project involves creating a combination calculator, but I'm facing issues with getting the output when using this script code. ...

Can you please explain the process of retrieving the value of an item from a drop-down menu using JavaScript?

I am currently developing a basic tax calculator that requires retrieving the value of an element from a drop-down menu (specifically, the chosen state) and then adding the income tax rate for that state to a variable for future calculations. Below is the ...

Detecting collisions using CSS animation

I'm currently working on a unique "game" project. Check out the code snippet here: jsfiddle function update() { coyote.applyForce(gravity); coyote.edges(); coyote.update(); cactus.update(); if (coyote.intersects(cactus)){ alert("colisio ...

Introducing the new and improved SweetAlert 2.0, the ultimate solution for

I am currently utilizing SweetAlert2.0, known as "This One" <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script> It is functioning properly; however, I would like to display a table, div, or HTML element within this swe ...

Modifying multiple heading tags simultaneously with jQuery

Currently utilizing jQuery to append a string to all heading tags (h1, h2,..., h6) and display it on the screen. Seeking guidance specifically for the replacing aspect, and open to solutions using plain javascript as well. The code I have so far, which I ...

Is there a way to adjust the dimensions of Google charts within a Bootstrap tab for a more customized appearance?

I've been working on a page that features tab navigation using Twitter Bootstrap and I want to include Google charts within each tab's content. However, I've encountered an issue where the charts are appearing in different sizes despite spec ...