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

MySql sets the specified user database view query permissions

1. Create a new user: 1. Execute SQL statement to...

A brief discussion on whether too many MySQL data queries will cause OOM

Table of contents Impact of full table scan on th...

MySQL 8.0.17 installation and usage tutorial diagram

Written in front In the past and in the current p...

Detailed explanation of how to monitor MySQL statements

Quick Reading Why do we need to monitor SQL state...

Briefly describe the four transaction isolation levels of MySql

Isolation Level: Isolation is more complicated th...

Detailed explanation of NodeJS modularity

Table of contents 1. Introduction 2. Main text 2....

A record of pitfalls in JS regular matching

I recently discovered a pitfall in regular expres...

JS cross-domain solution react configuration reverse proxy

Cross-domain solutions jsonp (simulate get) CORS ...

Install Kafka in Linux

Table of contents 1.1 Java environment as a prere...

A brief analysis of how to upgrade PHP 5.4 to 5.6 in CentOS 7

1. Check the PHP version after entering the termi...