Understanding and example code of Vue default slot

Understanding and example code of Vue default slot

What is a slot

A slot is a placeholder in a child component that is provided to a parent component, represented by <slot></slot>. The parent component can fill any template code in this placeholder, such as HTML, components, etc. The filled content will replace the <slot></slot> tag of the child component.

Understanding of default slots

Just use the full tag () to write the corresponding configuration in its full tag (for example: the functions we need)

Then use the default slot tag to put the written content into this slot (this slot generally exists in the child component, so you can give the child component what the parent component has written)

Regarding the configuration style written in the complete tag, we can write it in both the parent component and the child component (because, 1. When the style is written in the parent component, the style has been rendered and then put into the child component; 2. When the style is written in the child component, put the configuration into the slot, and the child component where the slot is located has a CSS style that will render our configuration)

Code Snippet

①Category.vue

<template>
  <div class="category">
    <h3>{{ title }}Category</h3>

    <!-- Define a default slot, then the content of the tag body in the corresponding component tag in App.vue will be placed in this slot-->
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: "Category",
  props: ["title"],
};
</script>

<style>
.category {
  background-color: skyblue;
  width: 200px;
  height: 300px;
}
h3 {
  text-align: center;
  background-color: orange;
}
</style>

②App.vue

<template>
  <div class="container">
    <Category title="Food">
      <img
        src="https://zqcdn.itzjj.cn/static/skin/mfw0321/static/picture/dj_scv.jpg"
        alt="1"
      />
    </Category>
    <Category title="Games" :listData="games">
      <ul>
        <!-- At this time, because the variables are directly in app.vue, you can directly traverse game
        After traversing, use the slot function to pass it to Category.vue -->
        <li v-for="(g, index) in games" :key="index">
          {{ g }}
        </li>
      </ul></Category
    >

    <Category title="Movies" :listData="films">
      <!-- controls allow the video to play -->
      <video
        Controls
        src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"
      ></video>
    </Category>
  </div>
</template>

<script>
import Category from "./components/Category";

export default {
  name: "App",
  components: { Category },
  data() {
    return {
      foods: ["fire", "your meat", "meatballs"],
      games: ["Red Alert Online", "Cross Fire", "Audition"],
      films: ["The Godfather", "Shock Wave", "Awesome"],
    };
  },
};
</script>

<style>
.container {
  display: flex;
  justify-content: space-around;
}

video
  width: 100%;
}

img {
  width: 100%;
}
</style>

Summarize

This is the end of this article about vue default slot. For more relevant vue default slot content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of anonymous slots and named slots in Vue
  • Basic usage examples of Vue named slots
  • Vue uses slots to distribute content operation examples [single slot, named slot, scoped slot]
  • Detailed explanation of how to use Vue anonymous, named and scoped slots
  • Detailed explanation of the usage of scoped slots in Vue.js slots
  • About VUE's compilation scope and slot scope slot issues
  • Detailed explanation of Vue scope slot implementation method and function
  • Vue default slot, named slot, scope slot definition and usage

<<:  Detailed process of deploying MySQL with docker (common applications deployed with docker)

>>:  How to set a dotted border in html

Recommend

Summarize some general principles of web design and production

<br />Related articles: 9 practical suggesti...

How to use less in WeChat applet (optimal method)

Preface I am used to writing less/sass, but now I...

How to install MySQL and MariaDB in Docker

Relationship between MySQL and MariaDB MariaDB da...

A detailed discussion of components in Vue

Table of contents 1. Component Registration 2. Us...

Simple example of adding and removing HTML nodes

Simple example of adding and removing HTML nodes ...

Tutorial diagram of installing zabbix2.4 under centos6.5

The fixed IP address of the centos-DVD1 version s...

Html tips to make your code semantic

Html semantics seems to be a commonplace issue. G...

Summary of learning Docker commands in one article

Table of contents Introduction Mirror repository ...

Attributes in vue v-for loop object

Table of contents 1. Values ​​within loop objects...

Detailed installation tutorial of mysql-8.0.11-winx64.zip

Download the zip installation package: Download a...

Detailed explanation of CSS margin collapsing

Previous This is a classic old question. Since a ...