This page assumes you’ve already read the Components Basics. Read that first if you are new to components.
- Use Slot Vue Js Online
- Use Slot Vue Json
- Use Slot Vue Jsp
- Use Slot Vuejs
- Use Slot Vue Js Download
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.
This allows you to compose components like this:
Then in the template for
<navigation-link>, you might have:
When the component renders, the
<slot> element will be replaced by “Your Profile”. Slots can contain any template code, including HTML:
Or even other components:
- It tells Vue which named slot to use, exposes the passed props, which we can destructure. This could be a method or data and works a bit like a render-function in React. Dynamic scoped slots. Right, the final piece of the puzzle. Dynamic scoped slots are much like regular scoped slots, but they have a name derived at runtime.
Correct way to pass props to all children through slot-scope in Vue.js. How to DRY use a Vue.js component with multiple props and slots. While basic component slots are all fine and dandy, sometimes you want the template inside the slot to be able to access data from the child component hosting the slot content. For example, if you’re trying to allow custom templates in a container while still retaining access to those containers’ data properties, you’ll want to use a. 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.
<navigation-link> did not contain a
<slot> element, any content passed to it would simply be discarded.
Use Slot Vue Js Online
There are times when it’s useful to have multiple slots. For example, in a hypothetical
base-layout component with the following template:
For these cases, the
<slot> element has a special attribute,
name, which can be used to define additional slots:
To provide content to named slots, we can use the
slot attribute on a
<template> element in the parent:
slot attribute can also be used directly on a normal element:
There can still be one unnamed slot, which is the default slot that serves as a catch-all outlet for any unmatched content. In both examples above, the rendered HTML would be:
Default Slot Content
There are cases when it’s useful to provide a slot with default content. For example, a
<submit-button> component might want the content of the button to be “Submit” by default, but also allow users to override with “Save”, “Upload”, or anything else.
To achieve this, specify the default content in between the
If the slot is provided content by the parent, it will replace the default content.
When you want to use data inside a slot, such as in:
Use Slot Vue Json
That slot has access to the same instance properties (i.e. the same “scope”) as the rest of the template. The slot does not have access to
<navigation-link>‘s scope. For example, trying to access
url would not work. As a rule, remember that:
Everything in the parent template is compiled in parent scope; everything in the child template is compiled in the child scope.
Use Slot Vue Jsp
New in 2.1.0+
Use Slot Vuejs
Sometimes you’ll want to provide a component with a reusable slot that can access data from the child component. For example, a simple
<todo-list> component may contain the following in its template:
But in some parts of our app, we want the individual todo items to render something different than just the
todo.text. This is where scoped slots come in.
Use Slot Vue Js Download
To make the feature possible, all we have to do is wrap the todo item content in a
<slot> element, then pass the slot any data relevant to its context: in this case, the
Now when we use the
<todo-list> component, we can optionally define an alternative
<template> for todo items, but with access to data from the child via the
slot-scope is no longer limited to the
<template> element, but can instead be used on any element or component in the slot.
The value of
This is a great way to make scoped slots a little cleaner.
← Custom EventsDynamic & Async Components →
Phát hiện lỗi hoặc muốn đóng góp vào nội dung? Chỉnh sửa trang này trên GitHub!