Nested Slots Vue Js

Nested Slots Vue Js 3,5/5 3722 votes

Ah, I guess defaults won’t work on the grandchild level, because the slot will always recieve at least an empty template from the child. You can have defaults on the child though. Nested scoped slots in Vue templates. GitHub Gist: instantly share code, notes, and snippets. The Most Important Feature in Vue November 2019. I've said it before and I'll say it again: Computed properties are the most important feature in Vue. Sure, scoped slots let you create some nice abstractions and watchers are useful too. But I don't think anything comes close to how valuable computed props are. This page assumes you’ve already read the Components Basics.Read that first if you are new to components. Vue implements a content distribution API that’s modeled after the current Web Components spec draft, using the slot element to serve as distribution outlets for content.

  1. Nested Slots Vue Js Tool
  2. Nested Slots Vue Js Login
  3. Nested Slots Vue Js Tutorial
  4. Nested Slots Vue Js 2.2

Nested Slots Vue Js Tool

Nested slots vue js 2.2

Tutorial

While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the 'report an issue' button at the bottom of the tutorial.

As your Vue.js Single Page Applications (SPAs) become moderately complex, you start to need Vue Router, and, moreover, nested routes. Nested routes allow for more complex user interfaces with components nested inside each other. Let’s build out a relatively simple use case that shows the utility of nested routes in Vue Router.

Setup with the Vue CLI

If you don’t have it installed already, install the Vue CLI globally by running either:

Or

Now you will be able to run the vue command from the command line. Let’s create a Vue application called alligator-nest:

Choose the default preset at the prompt (hit the enter key). After that, run the following command:

Nested slots vue js login2.2Slots

Then, go ahead and open up the alligator-nest directory in your editor of choice.

Base Code

The following CSS will help us with positioning elements for our UI. Add it as a stylesheet file in the public/ folder and reference it in public/index.html. We’ll be making use of CSS grid for this:

Next, let’s make some changes to the default files that vue-cli added.

Delete HelloWorld.vue from the src/components folder and delete any mention of it from src/App.vue. Make the following modifications to the HTML markup and CSS styling in App.vue.

If you run npm run serve in the project’s root directory you can mouse over to localhost:8080 in your browser and see a skeleton layout. Those display: grid properties are useful! Now we can begin routing.

Enter Vue Routing

Let’s whip up a component called AboutPage.vue in our /components folder. It will look like this:

Now our main.js file needs an /about route. It will look like this.

Finally let’s go back to App.vue and change the anchor tag for “About” to a <router-link> tag with an attribute of to='/about'. Then, change the second div to a <router-view> tag. Make sure to keep the grid positioning class attributes intact.

We now have a functional site skeleton with routing handled for the About page.

We’re focusing on the routing functionality here so we’re not going to get fancy with styling. Even so, the Travels page is going to look more elaborate.

To start, create a TravelPage the same way you created an AboutPage. Reference it in main.js.

Nested Slots Vue Js Login

Also create the following two components which will ultimately be nested in TravelPage.vue:

TravelAmericaPage.vue

Nested route configuration

Now, let’s update both main.js and TravelPage.vue to reference those nested routes using children. main.js must be updated to have the following definition for the routes constant:

Note that the nesting of children can continue infinitely.

And TravelPage.vue can be written in the following way:

TravelPage.vue

Check out localhost:8080 and you will see that the Travels page has 2 subpages within it! Our URLs update accordingly when you click on either link.

Conclusion

Hopefully this tutorial was useful for you in seeing how to get started with nested routes!

Other things to keep in mind on the topic — we could have routes defined with dynamic segments such as path: '/location/:id'. Then, on the views for those routes we can reference that id as this.$route.params. This is useful when you have more data of a particular type (users, pictures, etc.) that you are wishing to display on your site/app.

🐊 Later!

Real app UIs are usually composed of components that are nested multiple levels deep. It is also very common that the segments of a URL corresponds to a certain structure of nested components, for example:

With vue-router, it is very simple to express this relationship using nested route configurations.

Given the app we created in the last chapter:

The <router-view> here is a top-level outlet. It renders the component matched by a top level route. Similarly, a rendered component can also contain its own, nested <router-view>. For example, if we add one inside the User component's template:

To render components into this nested outlet, we need to use the children option in VueRouter constructor config:

Note that nested paths that start with / will be treated as a root path. This allows you to leverage the component nesting without having to use a nested URL.

As you can see the children option is just another Array of route configuration objects like routes itself. Therefore, you can keep nesting views as much as you need.

Nested Slots Vue Js Tutorial

At this point, with the above configuration, when you visit /user/foo, nothing will be rendered inside User's outlet, because no sub route is matched. Maybe you do want to render something there. In such case you can provide an empty subroute path:

Nested Slots Vue Js 2.2

A working demo of this example can be found here.

Comments are closed.